How to return a value from pthread threads in C?

后端 未结 8 2153
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 03:16

I\'am new to C and would like to play with threads a bit. I would like to return some value from a thread using pthread_exit()

My code is as follows:

相关标签:
8条回答
  • 2020-11-27 03:45

    You are returning a reference to ret which is a variable on the stack.

    0 讨论(0)
  • 2020-11-27 03:53
    #include<stdio.h>
    #include<pthread.h>
    void* myprint(void *x)
    {
     int k = *((int *)x);
     printf("\n Thread created.. value of k [%d]\n",k);
     //k =11;
     pthread_exit((void *)k);
    
    }
    int main()
    {
     pthread_t th1;
     int x =5;
     int *y;
     pthread_create(&th1,NULL,myprint,(void*)&x);
     pthread_join(th1,(void*)&y);
     printf("\n Exit value is [%d]\n",y);
    }  
    
    0 讨论(0)
提交回复
热议问题