-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads_conds_ex.c
81 lines (61 loc) · 1.76 KB
/
threads_conds_ex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by moezgen on 12/20/24.
//
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#define COUNT_DONE 10
#define COUNT_HALT1 3
#define COUNT_HALT2 6
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int count = 0;
void *thread_func1(void *arg) {
while (1) {
pthread_mutex_lock(&count_mutex);
// Wait until `count` reaches COUNT_HALT1 or COUNT_DONE
while (count < COUNT_HALT1) {
pthread_cond_wait(&cond, &count_mutex);
}
if (count >= COUNT_DONE) {
pthread_mutex_unlock(&count_mutex);
break;
}
count++;
printf("Counter value in func1: %d\n", count);
// Signal `thread_func2`
pthread_cond_signal(&cond);
pthread_mutex_unlock(&count_mutex);
}
return NULL;
}
void *thread_func2(void *arg) {
while (1) {
pthread_mutex_lock(&count_mutex);
// Wait until `count` reaches COUNT_HALT2 or COUNT_DONE
while (count >= COUNT_HALT1 && count < COUNT_HALT2) {
pthread_cond_wait(&cond, &count_mutex);
}
if (count >= COUNT_DONE) {
pthread_mutex_unlock(&count_mutex);
break;
}
count++;
printf("Counter value in func2: %d\n", count);
// Signal `thread_func1`
pthread_cond_signal(&cond);
pthread_mutex_unlock(&count_mutex);
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
// Create threads
pthread_create(&thread1, NULL, thread_func1, NULL);
pthread_create(&thread2, NULL, thread_func2, NULL);
// Wait for threads to complete
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}