-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition_synchronization.c
91 lines (70 loc) · 1.8 KB
/
condition_synchronization.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
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __APPLE__
#include <dispatch/dispatch.h>
#else
#include <semaphore.h>
#endif
#define LOOP_ITERATIONS 10000
#define MULTIPLIER 100
int main(int argc, char* argv[]);
void* createThreadA();
void* createThreadB();
int result = 0;
#ifdef __APPLE__
dispatch_semaphore_t semaphore;
#else
sem_t semaphore;
#endif
void* createThreadB(void* id)
{
// Thread B waits for the semaphore which will only allow thread B
// access once thread A finishes its work
#ifdef __APPLE__
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
#else
sem_wait(semaphore);
#endif
result = result * MULTIPLIER;
return NULL;
}
void* createThreadA(void* id)
{
// Simulate a long-running task...
sleep(1);
for(int i = 0; i < LOOP_ITERATIONS; i++)
result = result + 1;
// Once done, thread A increments the semaphore so that B can run
#ifdef __APPLE__
dispatch_semaphore_signal(semaphore);
#else
sem_post(semaphore);
#endif
return NULL;
}
int main(int argc, char* argv[])
{
pthread_t threadA;
pthread_t threadB;
// Create a semaphore with a starting value of 0.
// One thread A finishes its task, it increments the
// semaphore so that thread B can run.
#ifdef __APPLE__
semaphore = dispatch_semaphore_create(0);
#else
sem_init(&semaphore, 0, 0);
#endif
// Create the two threads
pthread_create(&threadA, NULL, createThreadA, NULL);
pthread_create(&threadB, NULL, createThreadB, NULL);
pthread_join(threadA, NULL);
pthread_join(threadB, NULL);
#ifndef __APPLE__
sem_close(&semaphore);
#endif
// Should be 1.000.000
printf("The result is: %d\n", result);
return 0;
}