// Compile with // gcc main.c ./tls.c -Xlinker --wrap=pthread_create -lpthread -DTLS -o test #include #include #include #include #include #include "./tls.h" PER_THREAD_MEMORY_START int x; PER_THREAD_MEMORY_END #define WORKERS 2 void *the_work(void *dummy) { unsigned long id = (unsigned long) dummy; int c; double d; printf("(%d) - Before work\n", id); c = READ_THREAD_VARIABLE(x); printf("(%d) - Variable x has value: %d\n", id, c); // ------ if (id == 0) { WRITE_THREAD_VARIABLE(x, 10); } else if (id == 1) { WRITE_THREAD_VARIABLE(x, 20); } // ------ printf("(%d) - After work\n", id); c = READ_THREAD_VARIABLE(x); printf("(%d) - Variable x has value: %d\n", id, c); return NULL; } int main(int argc, char** argv){ pthread_t tid; long int i; for (i = 0; i < WORKERS; i++){ pthread_create(&tid, NULL, the_work, (void*)i); } pause(); return 0; }