Download presentation
Presentation is loading. Please wait.
Published byLeo Horton Modified over 8 years ago
1
CS 360 pthreads Condition Variables for threads
2
Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining with it (to synchronize with its completion) is OK for some problems, but too restrictive for others. Creating a thread is relatively expensive and to minimize that overhead, it would be desirable to create a set of threads once, keeping them in a pool to be allocated to tasks. 4 Sort of like the OS does when allocating processors to processes
3
Page 3 CS 360, WSU Vancouver Pthread’s Condition Variables Not really a variable 4 It does not contain your program’s data Rather a means of allowing threads to: 4 Wait to be signaled (woken up) by another thread 4 Signal other waiting threads (no relation to UNIX signal() or kill() system facilities) What you make your threads wait for, and when you signal them is all up to you.
4
Page 4 CS 360, WSU Vancouver More… New pthread type: 4 pthread_cond_t 4 Think of it as a list of threads waiting to be signaled (woken up) Declare it as a global variable, or allocate it on the heap 4 It has to be shareable by all threads! Initialize it before use: pthread_cond_init(pthread_cond_t* someConditionVar);
5
Page 5 CS 360, WSU Vancouver Mutexes In most cases, you will need to protect access to a condition variable (and the shared data associated with it) with a pthread_mutex Your shared data that is being examined and your condition variable should not be viewed or modified by a thread unless it has exclusive access to it, hence get a lock on it…
6
Page 6 CS 360, WSU Vancouver Waiting for a condition… The following procedure: pthread_cond_wait(pthread_cond_t* myCond, pthread_mutex_t* mutex) Adds the calling thread to the list of threads waiting for “myCond” and suspends the calling thread until “myCond” is signaled. The associated mutex should already be locked by the calling thread. “myCond” is modified to add the calling thread, then the mutex is unlocked and the thread suspended. When (or if) the calling thread returns from pthread_cond_wait the mutex will have been locked for the thread.
7
Page 7 CS 360, WSU Vancouver Signaling a condition… The following procedure: pthread_cond_signal(pthread_cond_t* myCond); This procedure “wakes” up threads that are suspended waiting for “myCond”. Note that if multiple threads are waiting, only one will be able to lock its mutex and execute. The selection of which one gets the lock (after the calling thread gives it up) and continues execution is arbitrary. Note that no mutex variable is passed to the procedure, however, the calling thread must have exclusive access to “myCond” when calling this procedure. Therefore, you should lock the associate mutex variable before calling it (and unlock it afterwards, as your program logic requires). The calling thread is not suspended by calling pthread_cond_signal
8
Page 8 CS 360, WSU Vancouver Example The following code is executed by one or more threads which “produce” a object and store its address in global variable “task”. The object cannot be produced for consumption unless the global variable “task” is NULL first. Other threads seek to “consume” such objects, and may be waiting to be notified via a condition variable. /* global shared stuff, assume it is initialized*/ TaskData* task; pthread_cond_t wantTask; pthread_cond_t spaceForTask; pthread_mutex_t mutex;
9
Page 9 CS 360, WSU Vancouver Producer while (more tasks to produce) { /* do the work, producing a task for the consumers */ Task* temp = produceTask(); /* lock up the shared variable items */ pthread_mutex_lock(&mutex);/* lock up the data */ if (task != NULL) { /* wait until informed task variable is NULL */ pthread_cond_wait(&spaceForTask,&mutex); } assert(task == NULL); task = temp;/* post the task /* signal the consumers a task is ready */ pthread_cond_signal(&wantTask); pthread_mutex_unlock(&mutex);/* give up the lock */ }
10
Page 10 CS 360, WSU Vancouver Consumer while (1) { Task* temp = NULL; /* get a lock on the shared variables… */ pthread_mutex_lock(&mutex);/* get a lock on the data */ if (task == NULL) {/* wait for a task */ pthread_cond_wait(&wantTask,&mutex); } if (task != NULL) { temp = task;/* found a task, take it */ task = NULL; /* let the producers know there is room for more */ pthread_cond_signal(&spaceForTask); } pthread_mutex_unlock(&mutex);/* give up the lock */ if (temp) consumeTask(temp);/* do the work, consume task */ }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.