c – 使用共享变量10 pthreads

前端之家收集整理的这篇文章主要介绍了c – 使用共享变量10 pthreads前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题在于:

我想写一个创建10个线程的短程序,每个程序打印一个通过指针传递给线程函数的“id”.

该计划的完整代码如下:

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct params {
  6. pthread_mutex_t mutex;
  7. int id;
  8. };
  9.  
  10. typedef struct params params_t;
  11.  
  12. void* hello(void* arg){
  13. int id;
  14. pthread_mutex_lock(&(*(params_t*)(arg)).mutex);
  15. id = (*(params_t*)(arg)).id;
  16. pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
  17. printf("Hello from %d\n",id);
  18. }
  19.  
  20.  
  21. int main() {
  22. pthread_t threads[10];
  23. params_t params;
  24. pthread_mutex_init (&params.mutex,NULL);
  25.  
  26. int i;
  27. for(i = 0; i < 10; i++) {
  28. params.id = i;
  29. if(pthread_create(&threads[i],NULL,hello,&params));
  30. }
  31.  
  32. for(i = 0; i < 10; i++) {
  33. pthread_join(threads[i],NULL);
  34. }
  35.  
  36. return 0;
  37. }

假定的输出(按此顺序不必要):

  1. Hello from 0
  2. ....
  3. Hello from 9

实际结果是:

  1. Hello from 2
  2. Hello from 3
  3. Hello from 3
  4. Hello from 4
  5. Hello from 5
  6. Hello from 6
  7. Hello from 8
  8. Hello from 9
  9. Hello from 9
  10. Hello from 9

我试图将互斥锁放在hello()函数的不同位置,但它没有帮助.

我该如何实现线程同步?

编辑:假设结果不是必要的0 … 9它可以是这些数字的任意组合,但每一个应该只出现一次.

解决方法

有两个问题:

答:你正在使用锁,但主要不知道这个锁.

B.在这种情况下锁定是不够的.你想要的是线程通过相互发信号来协作(因为你希望main不会增加变量,直到一个线程说它完成打印它).您可以使用pthread_cond_t来实现此目的(Look here以了解有关此内容的更多信息).这归结为以下代码(基本上,我在代码添加了适当的pthread_cond_t用法,以及一堆解释正在发生的事情的注释):

  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct params {
  6. pthread_mutex_t mutex;
  7. pthread_cond_t done;
  8. int id;
  9. };
  10.  
  11. typedef struct params params_t;
  12.  
  13. void* hello(void* arg){
  14.  
  15. int id;
  16. /* Lock. */
  17. pthread_mutex_lock(&(*(params_t*)(arg)).mutex);
  18.  
  19. /* Work. */
  20. id = (*(params_t*)(arg)).id;
  21. printf("Hello from %d\n",id);
  22.  
  23. /* Unlock and signal completion. */
  24. pthread_mutex_unlock(&(*(params_t*)(arg)).mutex);
  25. pthread_cond_signal (&(*(params_t*)(arg)).done);
  26.  
  27. /* After signalling `main`,the thread could actually
  28. go on to do more work in parallel. */
  29. }
  30.  
  31.  
  32. int main() {
  33.  
  34. pthread_t threads[10];
  35. params_t params;
  36. pthread_mutex_init (&params.mutex,NULL);
  37. pthread_cond_init (&params.done,NULL);
  38.  
  39. /* Obtain a lock on the parameter. */
  40. pthread_mutex_lock (&params.mutex);
  41.  
  42. int i;
  43. for(i = 0; i < 10; i++) {
  44.  
  45. /* Change the parameter (I own it). */
  46. params.id = i;
  47.  
  48. /* Spawn a thread. */
  49. pthread_create(&threads[i],&params);
  50.  
  51. /* Give up the lock,wait till thread is 'done',then reacquire the lock. */
  52. pthread_cond_wait (&params.done,&params.mutex);
  53. }
  54.  
  55. for(i = 0; i < 10; i++) {
  56. pthread_join(threads[i],NULL);
  57. }
  58.  
  59. /* Destroy all synchronization primitives. */
  60. pthread_mutex_destroy (&params.mutex);
  61. pthread_cond_destroy (&params.done);
  62.  
  63. return 0;
  64. }

我看到你正在尝试的例子是一个可能了解POSIX线程库的玩具程序.在现实世界中,我们都知道,即​​使不使用线程,也可以更快地完成.但你已经知道了.

猜你在找的C&C++相关文章