Skip to content

Commit 246d7f5

Browse files
committed
Threads-Hybrid: Research for extern and __thread
1 parent 1a0456a commit 246d7f5

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
thread-local
22
udp-connect-client
33
udp-connect-server
4+
extern-main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#include <stdio.h>
3+
4+
int __thread ga = 100;
5+
int __thread gb = 200;
6+
7+
void* pfn2(void* arg)
8+
{
9+
printf("Thread2: ga=%d, gb=%d\n", ga, gb);
10+
return NULL;
11+
}
12+
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
g++ -std=c++11 -g -O0 extern-main.cpp extern-extra.cpp -o extern-main
3+
*/
4+
#include <stdio.h>
5+
// @see https://linux.die.net/man/3/pthread_create
6+
#include <pthread.h>
7+
8+
/*
9+
Main: ga=100, gb=1867710016
10+
Thread1: ga=100, gb=1867710016
11+
Thread2: ga=100, gb=200
12+
*/
13+
extern __thread int ga;
14+
extern int gb;
15+
16+
void* pfn(void* arg)
17+
{
18+
printf("Thread1: ga=%d, gb=%d\n", ga, gb);
19+
return NULL;
20+
}
21+
22+
extern void* pfn2(void* arg);
23+
24+
int main(int argc, char** argv)
25+
{
26+
printf("Main: ga=%d, gb=%d\n", ga, gb);
27+
28+
pthread_t trd = NULL;
29+
pthread_create(&trd, NULL, pfn, NULL);
30+
pthread_join(trd, NULL);
31+
32+
pthread_t trd2 = NULL;
33+
pthread_create(&trd2, NULL, pfn2, NULL);
34+
pthread_join(trd2, NULL);
35+
return 0;
36+
}

0 commit comments

Comments
 (0)