-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path08 - PThreads Joinable Mutexed improved.c
72 lines (55 loc) · 1.94 KB
/
08 - PThreads Joinable Mutexed improved.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
#include <pthread.h>
#include "Defs.h"
// Compilation:
// gcc -std=gnu99 -pthread ErrorHandling.c LogF.c "08 - PThreads Joinable Mutexed improved.c" -o joinable_threads_synced1
// Task:
// Simple example of an application working with 2 threads, where parent waits results from child.
// Thread 2 stops calculation if common value already more than limit.
// Access to variable is controlled with mutex, but located in the separate function.
static long get_and_incr_x(long incr)
{
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; // Must be static, because initialization function maybe not thread-safe.
// And C++ may even use lazy initialization for mutex.
static long x = 0;
// rtn and incr variables are on stack - i.e. local, so we need to use mutex for access to it.
long rtn;
ec_rv( pthread_mutex_lock(&mtx) )
rtn = x += incr; // Static variable - i.e. global. Needs to be protected.
ec_rv( pthread_mutex_unlock(&mtx) )
return rtn;
EC_CLEANUP_BGN
exit(EXIT_FAILURE);
EC_CLEANUP_END
}
static void* thread_func(void* arg)
{
long val = 0;
while (val < (long)arg)
{
val = get_and_incr_x(1); // Use local variable, because it is thread safe.
printf("Thread 2, counter value %ld\n", val);
sleep(1);
}
return (void *)val;
}
int main(void)
{
pthread_t tid;
void *status;
bool done;
assert(sizeof(long) <= sizeof(void *));
ec_rv( pthread_create(&tid, NULL, thread_func, (void *)6) )
long val = 0;
while (val < 10)
{
val = get_and_incr_x(1); // Use local variable, because it is thread safe.
printf("Thread 1, counter value %ld\n", val);
sleep(2);
}
ec_rv( pthread_join(tid, &status) )
printf("Return code of a Thread 2: %ld\n", (long)status);
return EXIT_SUCCESS;
EC_CLEANUP_BGN
return EXIT_FAILURE;
EC_CLEANUP_END
}