Aside client server transactions vs database

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t the last thread, then it goes to sleep and releases the mutex (line 24) so that other threads can enter the barrier. 11.5.4 Timeout Waiting Sometimes when we write a concurrent program, we are only willing to wait a finite amount of time for a particular condition to become true. Since a È operation can block indefinitely, this kind of timeout waiting is not possible to implement with semaphore operations. However, Pthreads provides this capability, in the form of the pthread cond timedwait function. 11.5. SYNCHRONIZING THREADS WITH MUTEX AND CONDITION VARIABLES 589 code/threads/barrier.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #include "csapp.h" static pthread_mutex_t mutex; static pthread_cond_t cond; static int nthreads; static int barriercnt = 0; void barrier_init(int n) { nthreads = n; Pthread_mutex_init(&mutex, NULL); Pthread_cond_init(&cond, NULL); } void barrier() { Pthread_mutex_lock(&mutex); if (++barriercnt == nthreads) { barriercnt = 0; Pthread_cond_broadcast(&cond); } else Pt...
View Full Document

Ask a homework question - tutors are online