Wrong thread IDs in a multithreaded C program?

后端 未结 2 1419
野趣味
野趣味 2021-01-21 22:53

I am new to multithreading in C and I had this question. I wrote the following code:

#include 
#include 
#include 

        
2条回答
  •  忘掉有多难
    2021-01-21 23:35

    You're passing the address of a variable the for is changing (i) so you're at the mercy of the scheduler. You should just pass a copy. As a cheap, not completely-kosher way:

    pthread_create(&thread[i],&attr,test, (void*)i);
    
    /* ... */
    
    int i = (int)a;
    

提交回复
热议问题