-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Threads-Hybrid: Research for extern and __thread
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
thread-local | ||
udp-connect-client | ||
udp-connect-server | ||
extern-main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
#include <stdio.h> | ||
|
||
int __thread ga = 100; | ||
int __thread gb = 200; | ||
|
||
void* pfn2(void* arg) | ||
{ | ||
printf("Thread2: ga=%d, gb=%d\n", ga, gb); | ||
return NULL; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
g++ -std=c++11 -g -O0 extern-main.cpp extern-extra.cpp -o extern-main | ||
*/ | ||
#include <stdio.h> | ||
// @see https://linux.die.net/man/3/pthread_create | ||
#include <pthread.h> | ||
|
||
/* | ||
Main: ga=100, gb=1867710016 | ||
Thread1: ga=100, gb=1867710016 | ||
Thread2: ga=100, gb=200 | ||
*/ | ||
extern __thread int ga; | ||
extern int gb; | ||
|
||
void* pfn(void* arg) | ||
{ | ||
printf("Thread1: ga=%d, gb=%d\n", ga, gb); | ||
return NULL; | ||
} | ||
|
||
extern void* pfn2(void* arg); | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
printf("Main: ga=%d, gb=%d\n", ga, gb); | ||
|
||
pthread_t trd = NULL; | ||
pthread_create(&trd, NULL, pfn, NULL); | ||
pthread_join(trd, NULL); | ||
|
||
pthread_t trd2 = NULL; | ||
pthread_create(&trd2, NULL, pfn2, NULL); | ||
pthread_join(trd2, NULL); | ||
return 0; | ||
} |