What is condition wait and signal in multi-threading?
What is condition wait and signal in multi-threading?
Explanation: When you want sleep a thread, condition variable can be used. In C under Linux, we have a function pthread_cond_wait() to wait or sleep.
On the other hand, we have a function pthread_cond_signal() to weak up sleeping or waiting thread.
Threads can wait on a condition variable.
Syntax of pthread_cond_wait():
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
Here,
cond : condition variable
mutex : is mutex lock
Return Value:
Upon successful completion, a value of zero shall be 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);
Here,
cond : condition variable
Return Value:
If successful, the pthread_cond_signal() functions shall return zero; otherwise, an error number shall be returned to indicate the error.
The pthread_cond_signal() weak up threads waiting for the condition variable.
The above two functions always works together.
The following sample program demonstrate the functionality of these function.
#include <stdio.h>
#include <pthread.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) {
// lets wait on conition varible cond1
done = 2;
printf("Waiting on condition variable cond1\n");
pthread_cond_wait(&cond1, &lock);
} else {
// Lets 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;
}
Posted by: Sacheen Prabhakar Birhade
Explanation: When you want sleep a thread, condition variable can be used. In C under Linux, we have a function pthread_cond_wait() to wait or sleep.
On the other hand, we have a function pthread_cond_signal() to weak up sleeping or waiting thread.
Threads can wait on a condition variable.
Syntax of pthread_cond_wait():
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
Here,
cond : condition variable
mutex : is mutex lock
Return Value:
Upon successful completion, a value of zero shall be 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);
Here,
cond : condition variable
Return Value:
If successful, the pthread_cond_signal() functions shall return zero; otherwise, an error number shall be returned to indicate the error.
The pthread_cond_signal() weak up threads waiting for the condition variable.
The above two functions always works together.
The following sample program demonstrate the functionality of these function.
#include <stdio.h>
#include <pthread.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) {
// lets wait on conition varible cond1
done = 2;
printf("Waiting on condition variable cond1\n");
pthread_cond_wait(&cond1, &lock);
} else {
// Lets 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;
}
Posted by: Sacheen Prabhakar Birhade
Comments
Post a Comment