I'm a little bit confused on how to initialize and implement a pthread mutex and condition variable. The goal of the program is to have producers place a set number of ints in a queue and consumers take the ints out of the queue. I must also be able to define the number of producer and consumer threads that are created. In the starter code, I am give these:
// Locks & Condition Variables
pthread_mutex_t lock; // Lock shared resources among theads
pthread_cond_t full; // Condition indicating queue is full
pthread_cond_t empty; // Condition indicating queue is empty
as shared resources. In the //TODO
comment in the main method, one of the steps says to initialize the lock and condition variables. I have a very weak understanding of pthread mutex's and conditions, so would I say:
lock = PTHREAD_MUTEX_INIT;
full = PTHREAD_MUTEX_INIT;
empty = PTHREAD_MUTEX_INIT;
In the consumer and producer methods, would i just call the lock by saying:
pthread_mutex_lock(&lock);
and
pthread_cond_wait(&full, &lock);
?
My code is pretty buggy right now, so I want to at least make sure that I'm using the mutex's and conditions correctly before debugging further. Thanks in advance!
A mutex can be statically initialized by assigning PTHREAD_MUTEX_INITIALIZER in its definition, as follows: pthread_mutex_t def_mutex = PTHREAD_MUTEX_INITIALIZER; A mutex must be initialized (either by calling pthread_mutex_init(), or statically) before it may be used in any other mutex functions.
While mutex implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met.
The pthread_cond_init() function initializes the specified condition variable. If attr is non-NULL, the attributes specified are used to initialize the condition variable. If the attribute object is modified later, the condition variable's attributes are not affected.
If you want to use the PTHREAD_XXX_INITIALIZER
macros you should use them in the variable declaration. Also use PTHREAD_COND_INITIALIZER
for condition variables:
// Locks & Condition Variables
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; // Lock shared resources among theads
pthread_cond_t full = PTHREAD_COND_INITIALIZER; // Condition indicating queue is full
pthread_cond_t empty = PTHREAD_COND_INITIALIZER; // Condition indicating queue is empty
Don't use those macros to initialize the mutex or condition variable later. If you need to do it later (for example if the object is dynamically allocated), use the appropriate init function:
pthread_mutex_init( &lock, NULL);
pthread_cond_init( &full, NULL);
pthread_cond_init( &empty, NULL);
To check a condition variable you must use a loop in order to avoid spurious unblocks and you must lock the mutex when:
pthread_cond_wait()
So whatever is waiting for an is-empty condition might look like:
pthread_mutex_lock(&lock);
while (!isEmpty) {
pthread_cond_wait(&empty, &lock);
}
// isEmpty is non-zero and the lock is held
Whatever is signalling that something is-empty might look like:
pthread_mutex_lock(&lock);
// ...
// we have emptied the queue while holding the lock
isEmpty = 1;
pthread_mutex_unlock(&lock);
pthread_cond_signal(&empty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With