#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
pthread_mutex_t lock; // 뮤텍스 선언
pthread_cond_t completion_signal; // 조건 변수 선언
int shared_resource = 0; // 공유 자원
int work_completed = 0; // 작업 완료 여부 플래그
void* thread_func(void* arg) {
pthread_mutex_lock(&lock); // 뮤텍스 락 획득
// Critical section
shared_resource++; // 공유 자원 수정
printf("Thread: Completed work\n"); // 스레드가 작업 완료 메시지 출력
work_completed = 1; // 작업 완료 플래그 설정
pthread_cond_signal(&completion_signal); // 완료를 통지
pthread_mutex_unlock(&lock); // 뮤텍스 락 해제
return NULL;
}
int main() {
pthread_t thread;
pthread_mutex_init(&lock, NULL); // 뮤텍스 초기화
pthread_cond_init(&completion_signal, NULL); // 조건 변수 초기화
pthread_create(&thread, NULL, thread_func, NULL); // 스레드 생성
pthread_mutex_lock(&lock); // 뮤텍스 락 획득
// 작업 완료를 기다림
while (!work_completed) {
pthread_cond_wait(&completion_signal, &lock); // 조건 변수를 사용하여 작업 완료를 대기
}
// 스레드 작업이 완료된 후에 공유 자원에 접근
printf("Main: Shared resource value: %d\n", shared_resource);
pthread_mutex_unlock(&lock); // 뮤텍스 락 해제
pthread_join(thread, NULL); // 스레드 종료 대기
pthread_mutex_destroy(&lock); // 뮤텍스 파괴
pthread_cond_destroy(&completion_signal); // 조건 변수 파괴
return 0;
}
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
// mutex 선언
pthread_mutex_t lock1, lock2;
// 스레드 간 신호를 위한 조건 변수
pthread_cond_t proceed_cond;
// 리소스 사용 여부
int resource1_used = 0, resource2_used = 0;
// 첫 번째 스레드의 함수
void *thread1_func(void *arg) {
pthread_mutex_lock(&lock1); // 리소스 1 잠금
printf("Thread 1: Holding resource 1\n");
resource1_used = 1; // r
pthread_cond_signal(&proceed_cond); // 조건 변수를 사용하여 대기 중인 스레드에게 신호 보내기
pthread_mutex_lock(&lock2); // 두 번째 리소스에 대한 뮤텍스 잠금
while (!resource2_used) { // 두 번째 리소스이 사용 가능할 때까지 대기
pthread_cond_wait(&proceed_cond, &lock2); // 조건 변수와 뮤텍스를 사용하여 대기
}
printf("Thread 1: Holding resource 2\n");
pthread_mutex_unlock(&lock2); // 두 번째 리소스에 대한 뮤텍스 잠금 해제
pthread_mutex_unlock(&lock1); // 첫 번째 리소스에 대한 뮤텍스 잠금 해제
pthread_exit(NULL);
}
// 두 번째 스레드의 함수
void *thread2_func(void *arg) {
pthread_mutex_lock(&lock2); // 두 번째 리소스에 대한 뮤텍스 잠금
printf("Thread 2: Holding resource 2\n");
resource2_used = 1; // 두 번째 리소스이 사용 중임을 표시
pthread_cond_signal(&proceed_cond); // 조건 변수를 사용하여 대기 중인 스레드에게 신호 보내기
pthread_mutex_lock(&lock1); // 첫 번째 리소스에 대한 뮤텍스 잠금
while (!resource1_used) { // 첫 번째 리소스이 사용 가능할 때까지 대기
pthread_cond_wait(&proceed_cond, &lock1); // 조건 변수와 뮤텍스를 사용하여 대기
}
printf("Thread 2: Holding resource 1\n");
pthread_mutex_unlock(&lock1); // 첫 번째 리소스에 대한 뮤텍스 잠금 해제
pthread_mutex_unlock(&lock2); // 두 번째 리소스에 대한 뮤텍스 잠금 해제
pthread_exit(NULL);
}
// 메인 함수
int main() {
pthread_t t1, t2;
// mutex 초기화
pthread_mutex_init(&lock1, NULL);
pthread_mutex_init(&lock2, NULL);
pthread_cond_init(&proceed_cond, NULL);
pthread_create(&t1, NULL, thread1_func, NULL); // 첫 번째 스레드 생성
pthread_create(&t2, NULL, thread2_func, NULL); // 두 번째 스레드 생성
pthread_join(t1, NULL); // 첫 번째 스레드 종료 대기
pthread_join(t2, NULL); // 두 번째 스레드 종료 대기
pthread_mutex_destroy(&lock1); // 첫 번째 뮤텍스 해제
pthread_mutex_destroy(&lock2); // 두 번째 뮤텍스 해제
pthread_cond_destroy(&proceed_cond); // 조건 변수 해제
return 0;
}
댓글