Initializing Mutexes and Condition Variables

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

RingBuffer.C
template<class Token>
RingBuffer<Token>::RingBuffer(unsigned int size) :
   buffer(size), filled(0), in(0), out(0)
{
   pthread_mutex_init(&mutex, 0);
   pthread_cond_init(&nonEmpty, 0);
   pthread_cond_init(&nonFull, 0);
} // constructor

*Both, mutex and condition variables need to be initialized.
 
*Mutexes are initially unlocked.
 
RingBuffer.C
template<class Token>
RingBuffer<Token>::~RingBuffer() {
   pthread_mutex_destroy(&mutex);
   pthread_cond_destroy(&nonEmpty);
   pthread_cond_destroy(&nonFull);
} // destructor

*Mutex and condition variables should be destroyed if they are no longer be used. This is best put into a destructor.
 
*Note that these operations must not be invoked if there are still other threads which depend on them.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 21, 2002