线程

来源:互联网 发布:华为的主要业务知乎 编辑:程序博客网 时间:2024/06/06 06:48
#include <stdio.h>   
  1. #include <stdlib.h>   
  2. #include <pthread.h>   
  3. #include <unistd.h>   
  4. #include <string.h>   
  5. //#define DEBUG 1   
  6. #define NUM 3   
  7.   
  8. int n=0;  
  9. pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量  
  10. pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//条件变量  
  11.   
  12.   
  13. void * thread_func(void *arg)  
  14. {  
  15.     int param=(int)arg;  
  16.     char c='A'+param;  
  17.     int ret,i=0;  
  18.     for (; i < 10; i++)  
  19.     {  
  20.         pthread_mutex_lock(&mylock);  
  21.         while (param != n)  //刚运行时,n = 0, param = 0,条件不成立,所以直接打印A  
  22.         {  
  23. #ifdef DEBUG   
  24.             printf("thread %d waiting\n", param);  
  25. #endif   
  26.             ret = pthread_cond_wait(&qready, &mylock);  
  27.             if (ret == 0)   
  28.             {  
  29. #ifdef DEBUG   
  30.                 printf("thread %d wait success\n", param);  
  31. #endif   
  32.             } else   
  33.             {  
  34. #ifdef DEBUG   
  35.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  36. #endif   
  37.             }  
  38.         }  
  39.        // printf("%d ",param+1);   
  40.         printf("%c ",c);  //打印A后  
  41.         n=(n+1)%NUM;      //n变成了1,对线程2会产出影响!!!!  
  42.         pthread_mutex_unlock(&mylock);  
  43.         //会唤醒所有的线程,因为当这个线程完后会等pthread_cond_wait()执行两次后才能退出while (param != n)  
  44.         pthread_cond_broadcast(&qready);  
  45.           
  46.     }      
  47.     return (void *)0;  
  48. }  
  49.   
  50. #if 0   
  51. //假设为线程2   
  52.   
  53. void * thread_func(void *arg)//传入值1  
  54. {  
  55.     int param=(int)arg;  
  56.     char c='A'+param;  
  57.     int ret,i=0;  
  58.     for (; i < 10; i++)  
  59.     {  
  60.         pthread_mutex_lock(&mylock);  
  61.         while (param != n)  //和线程1同时执行,所以刚开始时条件满足  
  62.         {  
  63. #ifdef DEBUG   
  64.             printf("thread %d waiting\n", param);  
  65. #endif   
  66.             //执行到此时,等待线程1发送信号,当线程1的A打印完后,n的值也变成了1,条件就不成立了  
  67.             ret = pthread_cond_wait(&qready, &mylock);  
  68.             if (ret == 0)   
  69.             {  
  70. #ifdef DEBUG   
  71.                 printf("thread %d wait success\n", param);  
  72. #endif   
  73.             } else   
  74.             {  
  75. #ifdef DEBUG   
  76.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  77. #endif   
  78.             }  
  79.         }  
  80.        // printf("%d ",param+1);  
  81.         printf("%c ",c); //此时打印值B  
  82.         n=(n+1)%NUM;    //对打印C的线程3产生影响!!!  
  83.         pthread_mutex_unlock(&mylock);  
  84.         pthread_cond_broadcast(&qready);  
  85.     }      
  86.     return (void *)0;  
  87. }  
  88.   
  89. #endif   
  90.   
  91. int main(int argc, char** argv) {  
  92.       
  93.     int i=0,err;  
  94.     pthread_t tid[NUM];  
  95.     void *tret;  
  96.     for(;i<NUM;i++)  
  97.     {  
  98.         err=pthread_create(&tid[i],NULL,thread_func,(void *)i);  
  99.         if(err!=0)  
  100.         {  
  101.             printf("thread_create error:%s\n",strerror(err));  
  102.             exit(-1);  
  103.         }  
  104.     }  
  105.     for (i = 0; i < NUM; i++)  
  106.     {  
  107.         err = pthread_join(tid[i], &tret);  
  108.         if (err != 0)  
  109.         {  
  110.             printf("can not join with thread %d:%s\n", i,strerror(err));  
  111.             exit(-1);  
  112.         }  
  113.     }  
  114.     printf("\n");  
  115.     return 0;  
  116. }  
[cpp] view plaincopyprint?
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <pthread.h>   
  4. #include <unistd.h>   
  5. #include <string.h>   
  6. //#define DEBUG 1   
  7. #define NUM 3   
  8.   
  9. int n=0;  
  10. pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量  
  11. pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//条件变量  
  12.   
  13.   
  14. void * thread_func(void *arg)  
  15. {  
  16.     int param=(int)arg;  
  17.     char c='A'+param;  
  18.     int ret,i=0;  
  19.     for (; i < 10; i++)  
  20.     {  
  21.         pthread_mutex_lock(&mylock);  
  22.         while (param != n)  //刚运行时,n = 0, param = 0,条件不成立,所以直接打印A  
  23.         {  
  24. #ifdef DEBUG   
  25.             printf("thread %d waiting\n", param);  
  26. #endif   
  27.             ret = pthread_cond_wait(&qready, &mylock);  
  28.             if (ret == 0)   
  29.             {  
  30. #ifdef DEBUG   
  31.                 printf("thread %d wait success\n", param);  
  32. #endif   
  33.             } else   
  34.             {  
  35. #ifdef DEBUG   
  36.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  37. #endif   
  38.             }  
  39.         }  
  40.        // printf("%d ",param+1);   
  41.         printf("%c ",c);  //打印A后  
  42.         n=(n+1)%NUM;      //n变成了1,对线程2会产出影响!!!!  
  43.         pthread_mutex_unlock(&mylock);  
  44.         //会唤醒所有的线程,因为当这个线程完后会等pthread_cond_wait()执行两次后才能退出while (param != n)  
  45.         pthread_cond_broadcast(&qready);  
  46.           
  47.     }      
  48.     return (void *)0;  
  49. }  
  50.   
  51. #if 0   
  52. //假设为线程2   
  53.   
  54. void * thread_func(void *arg)//传入值1  
  55. {  
  56.     int param=(int)arg;  
  57.     char c='A'+param;  
  58.     int ret,i=0;  
  59.     for (; i < 10; i++)  
  60.     {  
  61.         pthread_mutex_lock(&mylock);  
  62.         while (param != n)  //和线程1同时执行,所以刚开始时条件满足  
  63.         {  
  64. #ifdef DEBUG   
  65.             printf("thread %d waiting\n", param);  
  66. #endif   
  67.             //执行到此时,等待线程1发送信号,当线程1的A打印完后,n的值也变成了1,条件就不成立了  
  68.             ret = pthread_cond_wait(&qready, &mylock);  
  69.             if (ret == 0)   
  70.             {  
  71. #ifdef DEBUG   
  72.                 printf("thread %d wait success\n", param);  
  73. #endif   
  74.             } else   
  75.             {  
  76. #ifdef DEBUG   
  77.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  78. #endif   
  79.             }  
  80.         }  
  81.        // printf("%d ",param+1);  
  82.         printf("%c ",c); //此时打印值B  
  83.         n=(n+1)%NUM;    //对打印C的线程3产生影响!!!  
  84.         pthread_mutex_unlock(&mylock);  
  85.         pthread_cond_broadcast(&qready);  
  86.     }      
  87.     return (void *)0;  
  88. }  
  89.   
  90. #endif   
  91.   
  92. int main(int argc, char** argv) {  
  93.       
  94.     int i=0,err;  
  95.     pthread_t tid[NUM];  
  96.     void *tret;  
  97.     for(;i<NUM;i++)  
  98.     {  
  99.         err=pthread_create(&tid[i],NULL,thread_func,(void *)i);  
  100.         if(err!=0)  
  101.         {  
  102.             printf("thread_create error:%s\n",strerror(err));  
  103.             exit(-1);  
  104.         }  
  105.     }  
  106.     for (i = 0; i < NUM; i++)  
  107.     {  
  108.         err = pthread_join(tid[i], &tret);  
  109.         if (err != 0)  
  110.         {  
  111.             printf("can not join with thread %d:%s\n", i,strerror(err));  
  112.             exit(-1);  
  113.         }  
  114.     }  
  115.     printf("\n");  
  116.     return 0;  
  117. }  
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <pthread.h>  
  4. #include <unistd.h>  
  5. #include <string.h>  
  6. //#define DEBUG 1  
  7. #define NUM 3  
  8.   
  9. int n=0;  
  10. pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量  
  11. pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//条件变量  
  12.   
  13.   
  14. void * thread_func(void *arg)  
  15. {  
  16.     int param=(int)arg;  
  17.     char c='A'+param;  
  18.     int ret,i=0;  
  19.     for (; i < 10; i++)  
  20.     {  
  21.         pthread_mutex_lock(&mylock);  
  22.         while (param != n)  //刚运行时,n = 0, param = 0,条件不成立,所以直接打印A  
  23.         {  
  24. #ifdef DEBUG  
  25.             printf("thread %d waiting\n", param);  
  26. #endif  
  27.             ret = pthread_cond_wait(&qready, &mylock);  
  28.             if (ret == 0)   
  29.             {  
  30. #ifdef DEBUG  
  31.                 printf("thread %d wait success\n", param);  
  32. #endif  
  33.             } else   
  34.             {  
  35. #ifdef DEBUG  
  36.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  37. #endif  
  38.             }  
  39.         }  
  40.        // printf("%d ",param+1);  
  41.         printf("%c ",c);  //打印A后  
  42.         n=(n+1)%NUM;      //n变成了1,对线程2会产出影响!!!!  
  43.         pthread_mutex_unlock(&mylock);  
  44.         //会唤醒所有的线程,因为当这个线程完后会等pthread_cond_wait()执行两次后才能退出while (param != n)  
  45.         pthread_cond_broadcast(&qready);  
  46.           
  47.     }      
  48.     return (void *)0;  
  49. }  
  50.   
  51. #if 0  
  52. //假设为线程2  
  53.   
  54. void * thread_func(void *arg)//传入值1  
  55. {  
  56.     int param=(int)arg;  
  57.     char c='A'+param;  
  58.     int ret,i=0;  
  59.     for (; i < 10; i++)  
  60.     {  
  61.         pthread_mutex_lock(&mylock);  
  62.         while (param != n)  //和线程1同时执行,所以刚开始时条件满足  
  63.         {  
  64. #ifdef DEBUG  
  65.             printf("thread %d waiting\n", param);  
  66. #endif  
  67.             //执行到此时,等待线程1发送信号,当线程1的A打印完后,n的值也变成了1,条件就不成立了  
  68.             ret = pthread_cond_wait(&qready, &mylock);  
  69.             if (ret == 0)   
  70.             {  
  71. #ifdef DEBUG  
  72.                 printf("thread %d wait success\n", param);  
  73. #endif  
  74.             } else   
  75.             {  
  76. #ifdef DEBUG  
  77.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  78. #endif  
  79.             }  
  80.         }  
  81.        // printf("%d ",param+1);  
  82.         printf("%c ",c); //此时打印值B  
  83.         n=(n+1)%NUM;    //对打印C的线程3产生影响!!!  
  84.         pthread_mutex_unlock(&mylock);  
  85.         pthread_cond_broadcast(&qready);  
  86.     }      
  87.     return (void *)0;  
  88. }  
  89.   
  90. #endif  
  91.   
  92. int main(int argc, char** argv) {  
  93.       
  94.     int i=0,err;  
  95.     pthread_t tid[NUM];  
  96.     void *tret;  
  97.     for(;i<NUM;i++)  
  98.     {  
  99.         err=pthread_create(&tid[i],NULL,thread_func,(void *)i);  
  100.         if(err!=0)  
  101.         {  
  102.             printf("thread_create error:%s\n",strerror(err));  
  103.             exit(-1);  
  104.         }  
  105.     }  
  106.     for (i = 0; i < NUM; i++)  
  107.     {  
  108.         err = pthread_join(tid[i], &tret);  
  109.         if (err != 0)  
  110.         {  
  111.             printf("can not join with thread %d:%s\n", i,strerror(err));  
  112.             exit(-1);  
  113.         }  
  114.     }  
  115.     printf("\n");  
  116.     return 0;  

0 0
原创粉丝点击