1 | #include <threads.h>
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <TLS.h>
|
---|
4 | #include <unistd.h>
|
---|
5 |
|
---|
6 | #define THREAD_COUNT 1
|
---|
7 |
|
---|
8 | static _Thread_local int local_i;
|
---|
9 |
|
---|
10 | int thread_func(void *id_ptr)
|
---|
11 | {
|
---|
12 | int id = *(int*)id_ptr;
|
---|
13 |
|
---|
14 | local_i = 256;
|
---|
15 |
|
---|
16 | int pid = fork();
|
---|
17 |
|
---|
18 | printf("From thread:[%d] (%s), Address of i (thread local): %p (%d)\n",
|
---|
19 | id, pid == 0 ? "child" : "parent",
|
---|
20 | (void*)&local_i, local_i);
|
---|
21 |
|
---|
22 | snooze(10000);
|
---|
23 |
|
---|
24 | return 0;
|
---|
25 | }
|
---|
26 |
|
---|
27 | int main(void)
|
---|
28 | {
|
---|
29 | thrd_t id[THREAD_COUNT];
|
---|
30 | int arr[THREAD_COUNT];
|
---|
31 |
|
---|
32 | for (int i = 0; i < THREAD_COUNT; i++)
|
---|
33 | arr[i] = i;
|
---|
34 |
|
---|
35 | for(int i = 0; i < THREAD_COUNT; i++) {
|
---|
36 | thrd_create(&id[i], thread_func, &arr[i]);
|
---|
37 | }
|
---|
38 |
|
---|
39 | for(int i = 0; i < THREAD_COUNT; i++) {
|
---|
40 | thrd_join(id[i], NULL);
|
---|
41 | }
|
---|
42 | }
|
---|