pthread_create如何传递多个参数

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
pthread_create 如何传递多个参数
参照了网上的一些资料,就那么几个,还老有错误,火大了,调了半天才调通,水平不行! 可是网上那些有错误的代码也真够可恶的,那些转载的人难道就没有发现吗?坑爹啊! 不说了,直接上代码!
[cpp] view plaincopyprint?
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. 10. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 11. static pthread_cond_t recv_over = PTHREAD_COND_INITIALIZER; 12. static pthread_cond_t decode_over = PTHREAD_COND_INITIALIZER; 13. static pthread_cond_t play_over = PTHREAD_COND_INITIALIZER; 14. 15. void* receive(void*); 16. void* decode(void*); 17. void* play(void*); 18. 19. pthread_t tdec, tplay, trecv; 20. 21. struct mypara 22. { 23. 24. 25. }; 26. 27. int main(int argc, char** argv) 28. { int thread_id; char *thread_name;
64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. } 77. 78. void* decode(void* arg) 79. { 80. 81. 82. 83. 84. 85. 86. 87. to play 88. 89. 90. 91. } 92. 93. 94. void* play(void* arg) 95. { 96. 97. int ret; printf("Start play\n"); } printf("End decode\n"); pthread_exit(0); while (1) { pthread_cond_wait(&recv_over, &mutex); printf("Decoding...\n"); sleep(1); pthread_cond_broadcast(&decode_over); //inform player ready printf("Start decode\n"); } printf("End receive\n"); pthread_exit(0); sleep(2); pthread_mutex_lock(&mutex); while (1) { printf("Receiving...\n"); sleep(1); pthread_cond_signal(&recv_over); pthread_cond_wait(&decode_over, &mutex);
int t1 = 0, t2 = 0, t3 = 0; t1 = pthread_create(&trห้องสมุดไป่ตู้cv, NULL, receive, (void *)¶); if(t1 != 0) printf("Create thread receive error!\n");
t2 = pthread_create(&tdec, NULL, decode, NULL); if(t2 != 0) printf("Create thread decode error!\n");
29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. } 52.
struct mypara para; para.thread_id = 1; para.thread_name = "recv";
98. 99. 100. 101. while(1) { pthread_cond_wait(&decode_over, &mutex); //wait the signal from decoder 102. 103. 104. 105. 106. 107. 108. } } pthread_mutex_unlock(&mutex); printf("End play\n"); pthread_exit(0); printf("Playing...\n"); sleep(1);
t3 = pthread_create(&tplay, NULL, play, NULL); if(t3 != 0) printf("Create thread play error!\n");
pthread_join(trecv, NULL); pthread_join(tdec, NULL); pthread_join(tplay, NULL); printf("leave main\n"); exit(0);
53. void* receive(void* arg) 54. { 55. 56. 57. 58. 59. 60. 61. 62. 63. struct mypara *recv_para; recv_para = (struct mypara *)arg; i = (*recv_para).thread_id; s = (*recv_para).thread_name; printf("NO : %d Name : %s\n",i,s); printf("Start receive\n"); int i = 0; char *s = NULL;
相关文档
最新文档