What are conditional wait and signal in multi-threading ?
Explanation: When you want to sleep a thread, condition variable can be used. In C under Linux, there is a function pthread_cond_wait() to wait or sleep.
On the other hand, there is a function pthread_cond_signal() to wake up sleeping or waiting thread.
Threads can wait on a condition variable.
Prerequisite : Multithreading
Syntax of pthread_cond_wait() :
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
Parameter :
cond : condition variable mutex : is mutex lock
Return Value :
On success, 0 is returned ; otherwise, an error number shall be returned to indicate the error.
The pthread_cond_wait() release a lock specified by mutex and wait on condition cond variable.
Syntax of pthread_cond_signal() :
int pthread_cond_signal(pthread_cond_t *cond);
Parameter :
cond : condition variable
Return Value :
On success, 0 is returned ; otherwise, an error number shall be returned to indicate the error.
The pthread_cond_signal() wake up threads waiting for the condition variable.
Note : The above two functions works together.
Below is the implementation of condition, wait and signal functions.
// C program to implement cond(), signal() // and wait() functions #include <pthread.h> #include <stdio.h> #include <unistd.h> // Declaration of thread condition variable pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; // declaring mutex pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; int done = 1; // Thread function void* foo() { // acquire a lock pthread_mutex_lock(&lock;); if (done == 1) { // let's wait on conition variable cond1 done = 2; printf("Waiting on condition variable cond1\n"); pthread_cond_wait(&cond1;, &lock;); } else { // Let's signal condition variable cond1 printf("Signaling condition variable cond1\n"); pthread_cond_signal(&cond1;); } // release lock pthread_mutex_unlock(&lock;); printf("Returning thread\n"); return NULL; } // Driver code int main() { pthread_t tid1, tid2; // Create thread 1 pthread_create(&tid1;, NULL, foo, NULL); // sleep for 1 sec so that thread 1 // would get a chance to run first sleep(1); // Create thread 2 pthread_create(&tid2;, NULL, foo, NULL); // wait for the completion of thread 2 pthread_join(tid2, NULL); return 0; } |
Output :
Recommended Posts:
- Wait System Call in C
- fork() to execute processes from bottom to up using wait()
- Multithreading in C
- Multithreading in C++
- Conditional Statements | Shell Script
- C program to check if a given year is leap year using Conditional operator
- Getting System and Process Information Using C Programming and Shell in Linux
- fork() and Binary Tree
- Performance analysis of Row major and Column major order of storing arrays in C
- Static and Dynamic Libraries | Set 1
- Terminal Mail and Linux Security Mail
- Zombie and Orphan Processes in C
- C program to demonstrate fork() and pipe()
- Zombie Processes and their Prevention
- Initializing and Cache Mechanism in Linux Kernel
- fork() and memory shared b/w processes created using it
- Reset a lost Linux administrative password and Explanation
- dup() and dup2() Linux system call
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
- Network configuration and trouble shooting commands in Linux
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

