Last active
October 13, 2016 18:18
-
-
Save bshlgrs/b708816e87c647795e17af34b36efad8 to your computer and use it in GitHub Desktop.
This is a demonstration that threadlocal storage can be written to by other threads.
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
#include <pthread.h> | |
#include <stdio.h> | |
__thread int very_local = 5; | |
/* this function is run by the second thread */ | |
void *do_other_thing(void *very_local_pointer) | |
{ | |
printf("in thread 2, very_local is %d\n", very_local); | |
*(int*) very_local_pointer = 10; | |
return NULL; | |
} | |
int main() | |
{ | |
printf("very_local is %d\n", very_local); | |
very_local = 2; | |
printf("very_local is %d\n", very_local); | |
pthread_t other_thread; | |
// I'm omitting error handling here, which is bad of me. | |
pthread_create(&other_thread, NULL, do_other_thing, &very_local); | |
pthread_join(other_thread, NULL); | |
printf("very_local is %d\n", very_local); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment