Thread Synchronization - a condition variable

Condition variable

  Mutex used to synchronize threads to share access to data, then the condition variable is used to synchronize threads among the shared values ​​of the data. Condition variables provide a notification mechanism between threads: When a

Shared data reaches a certain value, the wake-up wait for this thread to share data.

  Correlation function condition variables are:

  int pthread_cond_init (pthread_cond_t * cond, const pthread_condattr_t * cond_attr), This function is used to initialize a condition variable, and sets the variable phase conditions

Off property.

  pthread_cond_destory (pthead_cond_t * cond), is used to destroy a condition variable, the release of kernel resources in its possession, the destruction is waiting for a condition variable will be back ebusy

mistake.

  a pthread_cond_broadcast () and a pthread_cond_signal () is used to wake up all the waiting condition variable wiring layer and the wake-up threads waiting for a condition variable. Not to notice

Interface specified condition variable.

  The pthread_cond_wait () wait for the condition variable, the second parameter is a mutex protected mutex variable conditions, ensure pthead_cond_wait () atomicity. Call pthread

_cond_wait before () To mutex must be locked to ensure pthead_cond_signal () and pthread_cond_broadcast () function during the function call does not modify the condition variable

The state will guarantee the state of the thread will not miss any condition variable.

  

Guess you like

Origin www.cnblogs.com/wangkaia/p/11949532.html