Title: COMP309/509 - Lecture 10 class: middle, center, inverse

COMP309/509 - Parallel and Distributed Computing

Lecture 11 - Thread Synchronization

By Mitchell Welch

University of New England


Reading


Summary


Conditional Variables


Conditional Variables

The wait function prototype is:

#include <pthread.h>
int pthread_cond_wait(pthread_cond_t  *cond, pthread_mutex_t *mutex);

Conditional Variables

pthread_cond_signal

or

pthread_cond_broadcast,

the mutex is reacquired before returning to the caller.


Conditional Variables

The signal function prototype is:

#include <pthread.h>
int pthread_cond_signal(pthread_cond_t *cond);

Conditional Variables


Conditional Variables

The broadcast prototype is:

#include <pthread.h>
int pthread_cond_broadcast(pthread_cond_t  *cond);

Synchronization Templates


Synchronization Templates

pthread_mutex_lock(&mutex);

while(!test_condition(...))
  pthread_cond_wait(&condvar, &mutex);

do_stuff(...);

pthread_mutex_unlock(&mutex);

Synchronization Templates


Synchronization Templates

pthread_mutex_lock(&mutex);
do_stuff(...);
pthread_cond_signal(&condvar);
pthread_mutex_unlock(&mutex);

Synchronization Examples


Synchronization Examples


Synchronization Examples


Synchronization Examples


Synchronization Examples


Producer-Consumer Problem Resolved


Producer-Consumer Problem Resolved


Producer-Consumer Problem Resolved


Producer-Consumer Problem Resolved


Producer-Consumer Problem Resolved


class: middle, center, inverse

Questions?


Summary


Reading