fork download
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4.  
  5. struct key_type {
  6. int value;
  7. key_type() : value(0) {
  8. sleep (1);
  9. pthread_yield();
  10. value = 42;
  11. }
  12. };
  13.  
  14.  
  15. void * thread1(void*) {
  16. static key_type local_key;
  17. printf("thread has key %d\n", local_key.value);
  18. return NULL;
  19. }
  20.  
  21. int main()
  22. {
  23. pthread_t t[2];
  24. pthread_create(&t[0], NULL, thread1, NULL);
  25. pthread_create(&t[1], NULL, thread1, NULL);
  26. pthread_join(t[0], NULL);
  27. pthread_join(t[1], NULL);
  28. }
Success #stdin #stdout 0s 85696KB
stdin
Standard input is empty
stdout
thread has key 42
thread has key 42