Pass integer value through pthread_create

前端 未结 4 778
感动是毒
感动是毒 2020-11-28 14:56

I simply want to pass the value of an integer to a thread.

How can I do that?

I tried:

    int i;
    pthread_t thread_tid[10];
    for(i=0;          


        
4条回答
  •  青春惊慌失措
    2020-11-28 15:14

    int is 32 bit, and void * is 64 bit in 64bit Linux; In that case you should use long int instead of int;

    long int i;
    pthread_create(&thread_id, NULL, fun, (void*)i);
    

    int fun(void *i) function

     long int id = (long int) i;
    

提交回复
热议问题