-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconditional.c
54 lines (37 loc) · 1.02 KB
/
conditional.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
/*
* Compiler flags: -Wall -Werror -Wextra -std=gnu11 -pthread
*/
#include <stdlib.h>
#include <pthread.h>
#define THREAD_COUNT 10
#define CONDITION 1
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void * thread() {
do {
pthread_mutex_lock(&mutex);
while(!CONDITION) {
pthread_cond_wait(&cond, &mutex);
}
//critial region
pthread_mutex_unlock(&mutex);
} while (CONDITION);
//return the sum of the read values
return NULL;
}
int main(int argc, char *argv[]) {
//storage for all threads
pthread_t open_threads[THREAD_COUNT];
//creating THREAD_COUNT threads
for (int i = 0; i < THREAD_COUNT; i++) {
pthread_create(open_threads + i, NULL, thread, NULL);
}
//joining up all threads and summing up the results
for (int i = 0; i < THREAD_COUNT; i++) {
pthread_join(open_threads[i], NULL);
}
//destroy mutex and conditional
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return EXIT_SUCCESS;
}