Hi there!

I am a student studying computer science.

시스템 프로그래밍

시스템 프로그래밍 7장 - Thread : Condition variable

만능성구 2020. 5. 6. 02:20
728x90

Condition variables

필요한 경우

mutex는 thread간 동기화를 제공한다.

- 어떤 thread가 mutex lock을 얻은 이후에 어떤 조건을 만족시킬 때까지 기다리는 경우가 있다.

- mutex를 얻은 이후에 condition을 위해 block된 상태가 길어지면 다른 thread들이 block을 얻을 수 없는 상태가 된다.

- block된 상태가 해결이 안되면 다른 thread들도 block된다.

- 따라서 모든 thread가 block되어서 교착상태가 될 수 있다.

 

그 block이 해제될 때까지 mutex lock을 release해주어야 좋다.

자신의 block을 풀고 어떻게 다시 lock을 가져오게 만들 것인가.

/* condition variable creation & destruction */
int pthread_cond_init (*condition, *attr);
int pthread_cond_destroy (*condition);
/* condition attribute creation & destruction */
int pthread_condattr_init (*attr);
int pthread_condattr_destroy (*attr);

pthread_cond_t *condition
pthread_condattr_t *attr

condition 변수와 속성을 설정해줄 수 있는 변수

condition variable 초기화 하는 함수

condition variable 제거해주는 함수

condition variable 속성을초기화 하는 함수

condition variable 속성 제거해주는 함수

 

변수에 대한 초기화가 이루어 져야 한다.

myconvar = PTHREAD_COND_INITIALIZER; 

codition

attr : 

- process-shared process간 variable 공유

     - allows the condition variable to be shared by other processes.

- set NULL to use default attributes

 return

Waiting / Signalling on CV

pthread_cond_wait (condition, mutex);
pthread_cond_signal (condition);
pthread_cond_broadcast (condition);

pthread_cond_t *condition
pthread_condattr_t *attr

초기화된 condition 변수를 이용해서 thread간에 signal을 주고 받는 것

pthread_cond_wait : 해당 condition 변수가 올 때까지 기다린다, mutex를 release해준다. 끝나면 다시 얻는다.

pthread_cond_signal : wait하고 있는 첫번째 process를 깨운다. 

pthread_cond_broadcast :  wait하고 있는 모든 process를 깨운다. 

wait가 아무도 없으면 signal은 사라진다.

condition : condition변수

mutex : mutex변수

 

return 
728x90