For each item it works for a while ie sleeprandmaxrand

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ed to as releasing the mutex. At any point in time, at most one thread can hold the mutex. 11.5.2 Condition Variables Condition variables are synchronization variables that are used for signaling. Pthreads denes three basic operations on condition variables. #include <pthread.h> int pthread cond init(pthread cond t *cond, pthread condattr t *attr); int pthread cond wait(pthread cond t *cond, pthread mutex t *mutex); int pthread cond signal(pthread cond t *cond); return: 0 if OK, nonzero on error 11.5. SYNCHRONIZING THREADS WITH MUTEX AND CONDITION VARIABLES 587 A condition variable cond must be initialized before it is used, either by calling pthread cond init or at compile-time: 1 pthread_cond_t cond = PTHREAD_COND_INITIALIZER; For our purposes the attr argument will always be NULL and can be safely ignored. A thread waits for some program condition associated with the condition variable cond to become true by calling pthread cond wait. In order to guarantee that the call to pthread cond wait is indivisible with respect to other instances of pthread cond wait and pthread cond signal, Pthreads requires that a mutex variable mutex be associated with the condition variable cond, and...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online