Skip to content

Instantly share code, notes, and snippets.

@bshlgrs
Last active October 13, 2016 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bshlgrs/b708816e87c647795e17af34b36efad8 to your computer and use it in GitHub Desktop.
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.
#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