CSC 357 Lecture Notes Week 10, Part 2
Details of Thread Synchronization
Final Exam Review
void push(Stack s, Value v) { s[cur_index] = v; cur_index++; }
pthread_mutex_t stack_lock = PTHREAD_MUTEX_INITIALIZER;
...
void push(Stack s, Value v) { pthread_mutex_lock(&stack_lock); s[cur_index] = v; cur_index++; pthread_mutex_unlock(&stack_lock); }
Shared Data:
pthread_cond_t c = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m = PTHREAD_COND_INITIALIZER;
bool some_condition;
Thread A: Thread B:
... ...
pthread_mutex_lock(&m); pthread_mutex_lock(&m);
while (!some_condition) { some_condition = TRUE;
pthread_cond_wait(&c, &m); pthread_mutex_unlock(&m);
} pthread_cond_signal(&c);
do_A_thing();
some_condition = FALSE; ...
pthread_mutex_unlock(&m);
...