-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path05 - PThreads.c
42 lines (33 loc) · 1.12 KB
/
05 - PThreads.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
#include <pthread.h>
#include "Defs.h"
// Compilation:
// gcc -std=gnu99 -pthread ErrorHandling.c LogF.c "05 - PThreads.c" -o pthreads
// Task:
// Simple example of a counter working in 2 threads. Example shows that sequence of ++ calls is not deterministic.
static long x = 0;
static void* thread_func(void* arg)
{
while (true)
{
printf("Thread 2, counter value %ld\n", ++x); // !!! WARNING!!! 2 Threads use the same variable. But increment operation is not atomic.
sleep(1);
}
}
int main(void)
{
pthread_t tid;
// Use macros ec_rv to determine returned error (if happened), because pthread doesn't use errno variable to set error info.
ec_rv(pthread_create(&tid,
NULL, // Use attributes by default. To create attribute, call pthread_attr_setscope or pthread_attr_setstaoksize
thread_func,
NULL))
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);
}
return EXIT_SUCCESS;
EC_CLEANUP_BGN
return EXIT_FAILURE;
EC_CLEANUP_END
}