0% found this document useful (0 votes)
59 views

Condition Variables

Condition variables allow threads to suspend execution and wait for a condition to be true. A condition variable must be associated with a mutex to prevent race conditions. Functions like pthread_cond_wait cause a thread to wait on a condition variable until another thread signals it using pthread_cond_signal or pthread_cond_broadcast. The example code demonstrates two threads incrementing a shared counter using a condition variable and mutex to coordinate access.

Uploaded by

hariharankalyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Condition Variables

Condition variables allow threads to suspend execution and wait for a condition to be true. A condition variable must be associated with a mutex to prevent race conditions. Functions like pthread_cond_wait cause a thread to wait on a condition variable until another thread signals it using pthread_cond_signal or pthread_cond_broadcast. The example code demonstrates two threads incrementing a shared counter using a condition variable and mutex to coordinate access.

Uploaded by

hariharankalyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

condition Variables:

A condition variable is a variable of type pthread_cond_t and is used with the


appropriate functions for waiting and later, process continuation. The condition
variable mechanism allows threads to suspend execution and relinquish the
processor until some condition is true. A condition variable must always be
associated with a mutex to avoid a race condition created by one thread
preparing to wait and another thread which may signal the condition before the
first thread actually waits on it resulting in a deadlock. The thread will be
perpetually waiting for a signal that is never sent. Any mutex can be used,
there is no explicit link between the mutex and the condition variable.
Man pages of functions used in conjunction with the condition variable:
Creating/Destroying:
o pthread_cond_init
o pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
o pthread_cond_destroy
Waiting on condition:
o pthread_cond_wait - unlocks the mutex and waits for the condition
variable cond to be signaled.
o pthread_cond_timedwait - place limit on how long it will block.
Waking thread based on condition:
o pthread_cond_signal - restarts one of the threads that are waiting
on the condition variable cond.

o pthread_cond_broadcast - wake up all threads blocked by the


specified condition variable.
Example code: cond1.c
01
02
03

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

04
05 pthread_mutex_t count_mutex
06 pthread_cond_t condition_var

= PTHREAD_MUTEX_INITIALIZER;
= PTHREAD_COND_INITIALIZER;

07
08

void *functionCount1();

09

void *functionCount2();

10

int count = 0;
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6

11
12
13
14
15
16
17

main()
{
pthread_t thread1, thread2;

18
19
20

pthread_create( &thread1, NULL, &functionCount1, NULL);


pthread_create( &thread2, NULL, &functionCount2, NULL);

21
22
23

pthread_join( thread1, NULL);


pthread_join( thread2, NULL);

24
25

printf("Final count: %d\n",count);

26
27
28

exit(EXIT_SUCCESS);
}

29
30 // Write numbers 1-3 and 8-10 as permitted by functionCount2()
31
32
33

void *functionCount1()
{

34
35
36
37

for(;;)
{
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );

38
39
40
41
42
43

// Wait while functionCount2() operates on count


// mutex unlocked if condition varialbe in functionCount2()
signaled.
pthread_cond_wait( &condition_var, &count_mutex );
count++;
printf("Counter value functionCount1: %d\n",count);

44
45

pthread_mutex_unlock( &count_mutex );

46
47
48
49

if(count >= COUNT_DONE) return(NULL);


}
}

50
51

// Write numbers 4-7

52
53
54
55
56
57

void *functionCount2()
{
for(;;)
{
pthread_mutex_lock( &count_mutex );

58
59
60
61
62
63
64
65
66
67
68
69
70

if( count < COUNT_HALT1 || count > COUNT_HALT2 )


{
// Condition of if statement has been met.
// Signal to free waiting thread by freeing the mutex.
// Note: functionCount1() is now permitted to modify "count".
pthread_cond_signal( &condition_var );
}
else
{
count++;
printf("Counter value functionCount2: %d\n",count);
}

71
72
73

pthread_mutex_unlock( &count_mutex );

74
75

if(count >= COUNT_DONE) return(NULL);


}

76
77

Compile: cc -pthread cond1.c (or cc -lpthread cond1.c for older versions of the
GNU compiler which explicitly reference the library)
Run: ./a.out
Results:
Counter value functionCount1:
Counter value functionCount1:
Counter value functionCount1:
Counter value functionCount2:
Counter value functionCount2:
Counter value functionCount2:
Counter value functionCount2:
Counter value functionCount1:
Counter value functionCount1:
Counter value functionCount1:
Final count: 10

1
2
3
4
5
6
7
8
9
10

Note that functionCount1() was halted while count was between the values
COUNT_HALT1 and COUNT_HALT2. The only thing that has been ensures is
thatfunctionCount2 will increment the count between the values
COUNT_HALT1 and COUNT_HALT2. Everything else is random.
The logic conditions (the "if" and "while" statements) must be chosen to insure
that the "signal" is executed if the "wait" is ever processed. Poor software logic
can also lead to a deadlock condition.
Note: Race conditions abound with this example because count is used as the
condition and can't be locked in the while statement without causing deadlock.

You might also like