1 / 7

Chapter 30 Condition Variables

Chapter 30 Condition Variables. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. Basic Ideas. A thread check whether a condition is true before continuing (parent waits for child) void *child(void * arg ) { printf (&quot;child<br>&quot;) ; / / XXX how to indicate we are done? return NULL; }

deva
Download Presentation

Chapter 30 Condition Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 30Condition Variables Chien-Chung Shen CIS, UD cshen@cis.udel.edu

  2. Basic Ideas • A thread check whether a condition is true before continuing (parent waits for child) void *child(void *arg) { printf("child\n"); // XXX how to indicate we are done? return NULL; } main(intargc, char *argv[]) {printf("parent: begin\n");pthread_tc;Pthread_create(&c, NULL, child, NULL); // create child // XXX how to wait for child? printf("parent: end\n"); return 0; }

  3. How to Wait for a Condition ? • “Shared” (global) variable volatile int done = 0; void *child(void *arg) { printf("child\n"); done = 1; return NULL; } main(intargc, char *argv[]) {printf("parent: begin\n");pthread_t c;Pthread_create(&c, NULL, child, NULL); // create child while (done == 0) ; // spin printf("parent: end\n"); return 0; } • Drawback • spin and waste CPU time

  4. How to Wait for a Condition ? • Condition variable • idea: some way to put the parent to sleep until the condition we are waiting for (e.g., the child is done executing) comes true • an explicit queuethat threads can put themselves on when some state of execution is notas desired (by waiting on the condition) • some other thread, when it changes said state, wake one (or more) of those waiting threads and allow them to continue

  5. Pthread Wait • pthread_cond_wait(&c, &m) • release the lock (m) and put the calling thread to sleep (atomically) • when the thread wakes up (after some other thread has signaled it), it must reacquire the lock (m) before returning to the caller • prevent race condition when a thread is trying to put itself to sleep

  6. Pthread Condition Variable int done = 0;pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t c = PTHREAD_COND_INITIALIZER; void thr_exit() { Pthread_mutex_lock(&m); done = 1; Pthread_cond_signal(&c); Pthread_mutex_unlock(&m); } void *child(void *arg) { printf("child\n"); thr_exit();return NULL; } void thr_join() { Pthread_mutex_lock(&m); while(done == 0) Pthread_cond_wait(&c, &m); // release m when sleeping (atomically) // reacquire m when wake up Pthread_mutex_unlock(&m); } main(intargc, char *argv[]) { printf("parent: begin\n");pthread_tp; Pthread_create(&p, NULL, child, NULL); thr_join(); printf("parent: end\n"); return 0; }

  7. Will the Following Work ? void thr_exit() { Pthread_mutex_lock(&m) Pthread_cond_signal(&c); Pthread_mutex_unlock(&m); } • Do we need done? • failed when child runs immediately and calls thr_exit() void thr_exit(){ done = 1; Pthread_cond_signal(&c); } • Does not hold a lock when wait and signal? • Failed when parent proceeds first (and will sleep forever) void thr_join() { Pthread_mutex_lock(&m); Pthread_cond_wait(&c, &m); Pthread_mutex_unlock(&m); } void thr_join() { if (done == 0) Pthread_cond_wait(&c); } interrupt

More Related