向线程中传递结构体并返回

  1 #include "head.h"
  2 
  3 struct node
  4 {
  5     int data;
  6     char ch;
  7 };
  8 
  9 void *func(void *arg)
 10 {
 11     struct node *mynode2;
 12 
 13     mynode2 = (struct node *)(arg);
 14     mynode2->data = 1;
 15     mynode2->ch = 'c';
 16 
 17     return mynode2;
 18 }
 19 
 20 int main()
 21 {
 22     struct node *mynode,*mynode1;
 23     int error;
 24     pthread_t tid;
 25 
 26     mynode = (struct node*)malloc(sizeof(struct node));
 27     if (error = pthread_create(&tid, NULL, func, mynode))
 28     {
 29         fprintf(stderr, "Failed to create thread:%s\n",strerror(error));
 30         return 0;
 31     }
 32     if (error = pthread_join(tid, (void **)&mynode1))
 33     {
 34         fprintf(stderr, "Failed to create thread:%s\n",strerror(error));
 35         return 0;
 36     }
 37     printf("mynode2:%d,%c\n",mynode1->data,mynode1->ch);
 38     }
posted @ 2012-10-11 09:58  SA高处不胜寒  阅读(659)  评论(0编辑  收藏  举报