previous | start | next

Mutual Exclusion

In part 2 of the program assignment, you have to deal with operations that modify the database - add and delete - not just queries.

With multiple threads accessing the same database structure, some mutual exclusion is required in order not to corrupt the database structure.

The POSIX threads provide several functions to achieve mutual exclusion, the simplest being:

      pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; // shared 
 
      pthread_mutex_lock(&m);

      pthread_mutex_unlock(&m);
   

Code to be executed by only one thread at a time should be surrounded by the two calls.

The pthread_mutex_lock(&m) call blocks the caller if any other thread has obtained the lock and not yet released it.



previous | start | next