Skip to content

Commit

Permalink
Threads-Hybrid: Research for extern and __thread
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 1a0456a commit 246d7f5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions trunk/research/thread-model/.gitignore
@@ -1,3 +1,4 @@
thread-local
udp-connect-client
udp-connect-server
extern-main
13 changes: 13 additions & 0 deletions trunk/research/thread-model/extern-extra.cpp
@@ -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;
}


36 changes: 36 additions & 0 deletions trunk/research/thread-model/extern-main.cpp
@@ -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;
}

0 comments on commit 246d7f5

Please sign in to comment.