pthread_create如何传递多个参数

http://zhidao.baidu.com/question/315398992.html

涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程

定义一个结构体
struct mypara
{
       var para1;//参数1
       var para2;//参数2
}
将这个结构体指针,作为void *形参的实际参数传递
struct mypara pstru;
pthread_create(&ntid, NULL, thr_fn,& (pstru));
函数中需要定义一个mypara类型的结构指针来引用这个参数 
void *thr_fn(void *arg)
{
       mypara *pstru;
       pstru = (* struct mypara) arg;
       pstru->para1;//参数1
       pstru->para2;//参数2

}

pthread_create函数接受的参数只有一个void *型的指针,这就意味着你只能通过结构体封装超过一个以上的参数作为一个整体传递。这是pthread_create函数的接口限定的,别人已经明确表明我只接受一个参数,你硬要塞给他两个肯定会出错了。所以通过结构体这种组合结构变通一下,同样实现了只通过一个参数传递,但通过结构指针对结构数据成员的引用实现多参数的传递

这种用结构体封装多参数的用法不仅仅用在pthread_create函数中,如果你自己设计的函数需要的参数很多〉=5个以上,都可以考虑使用结构体封装,这样对外你的接口很简洁清晰,你的函数的消费者使用起来也很方便,只需要对结构体各个成员赋值即可,避免了参数很多时漏传、误传(参数串位)的问题

结构体内包含结构体完全没有问题,很多应用都这么使用


举例如下:

http://wenku.baidu.com/view/48a302ed6294dd88d0d26b73.html

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<pthread.h>  
  4. #include<errno.h>  
  5. #include<unistd.h>  
  6.   
  7. typedef void* (*fun)(void*);    
  8.   
  9. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  
  10. static pthread_cond_t recv_over = PTHREAD_COND_INITIALIZER;  
  11. static pthread_cond_t decode_over = PTHREAD_COND_INITIALIZER;  
  12. static pthread_cond_t play_over = PTHREAD_COND_INITIALIZER;  
  13.   
  14. void* receive(void*);  
  15. void* decode(void*);  
  16. void* play(void*);  
  17.   
  18. pthread_t tdec, tplay, trecv;  
  19.   
  20. struct mypara   
  21. {   
  22.     int thread_id;  
  23.     char *thread_name;   
  24. };  
  25.   
  26. int main(int argc, char** argv)  
  27. {  
  28.     struct mypara para;  
  29.     para.thread_id = 1;  
  30.     para.thread_name = "recv";  
  31.       
  32.     int t1 = 0, t2 = 0, t3 = 0;  
  33.     t1 = pthread_create(&trecv, NULL, receive,& (para));  
  34.     if(t1 != 0)  
  35.         printf("Create thread receive error!\n");  
  36.       
  37.     t2 = pthread_create(&tdec, NULL, decode, NULL);  
  38.     if(t2 != 0)  
  39.         printf("Create thread decode error!\n");  
  40.   
  41.     t3 = pthread_create(&tplay, NULL, play, NULL);  
  42.     if(t3 != 0)  
  43.         printf("Create thread play error!\n");  
  44.   
  45.     pthread_join(trecv, NULL);  
  46.     pthread_join(tdec, NULL);  
  47.     pthread_join(tplay, NULL);  
  48.     printf("leave main\n");  
  49.     exit(0);      
  50. }  
  51.   
  52. void* receive(void* arg)  
  53. {  
  54.     printf("Start receive\n");  
  55.     int i = 0;  
  56.     char *s = NULL;  
  57.   
  58.     struct mypara *recv_para;  
  59.     recv_para = (struct mypara *)arg;  
  60.     i = (*recv_para).thread_id;  
  61.     s = (*recv_para).thread_name;  
  62.     printf("NO : %d Name : %s\n",i,s);  
  63.       
  64.     sleep(2);  
  65.     pthread_mutex_lock(&mutex);  
  66.     while (1)  
  67.     {  
  68.         printf("Receiving...\n");  
  69.         sleep(1);  
  70.         pthread_cond_signal(&recv_over);  
  71.         pthread_cond_wait(&decode_over, &mutex);   
  72.     }  
  73.     printf("End receive\n");  
  74.     pthread_exit(0);  
  75. }  
  76.   
  77. void* decode(void* arg)  
  78. {  
  79.     printf("Start decode\n");  
  80.       
  81.     while (1)  
  82.     {  
  83.         pthread_cond_wait(&recv_over, &mutex);   
  84.         printf("Decoding...\n");  
  85.         sleep(1);  
  86.         pthread_cond_broadcast(&decode_over);   //inform player ready to play  
  87.     }  
  88.     printf("End decode\n");  
  89.     pthread_exit(0);  
  90. }  
  91.   
  92.   
  93. void* play(void* arg)  
  94. {  
  95.     int ret;  
  96.     printf("Start play\n");  
  97.   
  98.     while(1)  
  99.     {  
  100.         pthread_cond_wait(&decode_over, &mutex); //wait the signal from decoder  
  101.         printf("Playing...\n");  
  102.         sleep(1);  
  103.     }  
  104.     pthread_mutex_unlock(&mutex);  
  105.     printf("End play\n");  
  106.     pthread_exit(0);  
  107. }  


  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值