Skip to content

Instantly share code, notes, and snippets.

@jbwyme
Created March 11, 2014 05:09
Show Gist options
  • Save jbwyme/9479813 to your computer and use it in GitHub Desktop.
Save jbwyme/9479813 to your computer and use it in GitHub Desktop.
pthreads with thread local storage
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
pthread_key_t key;
struct args {
char *msg;
};
void print_msg(char *msg) {
int *tl = (int *)pthread_getspecific(key);
printf("msg: %s, tl: %d", msg, *tl);
}
void *exec_in_thread(struct args *args) {
int *tl = malloc(sizeof(int));
*tl = 5;
pthread_setspecific(key, tl);
print_msg(args->msg);
sleep(2);
*tl = 4;
print_msg(args->msg);
pthread_setspecific(key, NULL);
free(tl);
pthread_exit(NULL);
}
int main() {
int i = 0, num_threads = 10;
pthread_t threads[num_threads];
struct args *my_args = malloc(sizeof(struct args));
my_args->msg = "some message...";
pthread_key_create(&key, NULL);
for(i = 0; i<num_threads; i++) {
pthread_create(&threads[i], NULL, exec_in_thread, my_args);
}
for(i = 0; i<num_threads; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment