-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path06 - PThreads Joinable.c
47 lines (35 loc) · 1.16 KB
/
06 - PThreads Joinable.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
#include <pthread.h>
#include "Defs.h"
// Compilation:
// gcc -std=gnu99 -pthread ErrorHandling.c LogF.c "06 - PThreads Joinable BAD.c" -o joinable_threads
// Task:
// Simple example of an application working with 2 threads, where parent WAITS for a results from child.
// Thread 2 stops calculation if common value already more than limit.
static long x = 0;
static void* thread_func(void* arg)
{
while (x < (long)arg)
{
printf("Thread 2, counter value %ld\n", ++x); // !!! WARNING!!! 2 Threads use the same variable. But increment operation is not atomic.
sleep(1);
}
return (void *)x;
}
int main(void)
{
pthread_t tid;
void *status;
assert(sizeof(long) <= sizeof(void *));
ec_rv( pthread_create(&tid, NULL, thread_func, (void *)6) )
while (x < 10)
{
printf("Thread 1, counter value %ld\n", ++x); // !!! WARNING!!! 2 Threads use the same variable. But increment operation is not atomic.
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
}