code_junkie

Help with basic threading concept / race conditions

Nov 13th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. void *thread_main(void *thread_number) {
  2. printf("In thread number %d.n", *(int *)thread_number);
  3. }
  4.  
  5. void main() {
  6. int i = 0;
  7. pthread_t thread;
  8.  
  9. for( i = 0; i < 10; i++ ) {
  10. printf("Creating thread %d.n");
  11. pthread_create(&thread, 0, thread_main, &i);
  12. printf("Created thread %d.n");
  13. }
  14. }
  15.  
  16. > Creating thread 1.
  17. > In thread number 1.
  18. > Created thread 1.
  19. > Creating thread 2.
  20. > In thread number 2.
  21. > Created thread 2.
  22.  
  23. for( i = 0; i < 10; i++ ) {
  24. int *thread_count = malloc(sizeof(int));
  25. *thread_count = i;
  26. printf("Creating thread %d.n", i);
  27. pthread_create(&thread, 0, thread_main, thread_count);
  28. printf("Created thread %d.n", i);
  29. }
  30.  
  31. void *thread_main(void *thread_number) {
  32. printf("In thread number %d.n", *(int *)thread_number);
  33. free(thread_numbr);
  34. }
Add Comment
Please, Sign In to add comment