1. 程式人生 > >編寫一個程式,開啟3個執行緒,這3個執行緒的ID分別為A、B、C,每個執行緒將自己的ID在螢幕上列印10遍

編寫一個程式,開啟3個執行緒,這3個執行緒的ID分別為A、B、C,每個執行緒將自己的ID在螢幕上列印10遍

#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. int n=0;  
  8. pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量
  9. pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//條件變數
  10. void * thread_func(void *arg)  
  11. {  
  12. int param=(int)arg;  
  13. char c='A'+param;  
  14. int ret,i=0;  
  15. for (; i < 10; i++)  
  16.     {  
  17.         pthread_mutex_lock(&mylock);  
  18. while (param != n)  //剛執行時,n = 0, param = 0,條件不成立,所以直接列印A
  19.         {  
  20. #ifdef DEBUG
  21.             printf("thread %d waiting\n", param);  
  22. #endif
  23.             ret = pthread_cond_wait(&qready, &mylock);  
  24. if (ret == 0)   
  25.             {  
  26. #ifdef DEBUG
  27.                 printf("thread %d wait success\n", param);  
  28. #endif
  29.             } else
  30.             {  
  31. #ifdef DEBUG
  32.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  33. #endif
  34.             }  
  35.         }  
  36. // printf("%d ",param+1);
  37.         printf(
    "%c ",c);  //列印A後
  38.         n=(n+1)%NUM;      //n變成了1,對執行緒2會產出影響!!!!
  39.         pthread_mutex_unlock(&mylock);  
  40. //會喚醒所有的執行緒,因為當這個執行緒完後會等pthread_cond_wait()執行兩次後才能退出while (param != n)
  41.         pthread_cond_broadcast(&qready);  
  42.     }      
  43. return (void *)0;  
  44. }  
  45. #if 0
  46. //假設為執行緒2
  47. void * thread_func(void *arg)//傳入值1
  48. {  
  49. int param=(int)arg;  
  50. char c='A'+param;  
  51. int ret,i=0;  
  52. for (; i < 10; i++)  
  53.     {  
  54.         pthread_mutex_lock(&mylock);  
  55. while (param != n)  //和執行緒1同時執行,所以剛開始時條件滿足
  56.         {  
  57. #ifdef DEBUG
  58.             printf("thread %d waiting\n", param);  
  59. #endif
  60. //執行到此時,等待執行緒1傳送訊號,當執行緒1的A列印完後,n的值也變成了1,條件就不成立了
  61.             ret = pthread_cond_wait(&qready, &mylock);  
  62. if (ret == 0)   
  63.             {  
  64. #ifdef DEBUG
  65.                 printf("thread %d wait success\n", param);  
  66. #endif
  67.             } else
  68.             {  
  69. #ifdef DEBUG
  70.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  71. #endif
  72.             }  
  73.         }  
  74. // printf("%d ",param+1);
  75.         printf("%c ",c); //此時列印值B
  76.         n=(n+1)%NUM;    //對列印C的執行緒3產生影響!!!
  77.         pthread_mutex_unlock(&mylock);  
  78.         pthread_cond_broadcast(&qready);  
  79.     }      
  80. return (void *)0;  
  81. }  
  82. #endif
  83. int main(int argc, char** argv) {  
  84. int i=0,err;  
  85.     pthread_t tid[NUM];  
  86. void *tret;  
  87. for(;i<NUM;i++)  
  88.     {  
  89.         err=pthread_create(&tid[i],NULL,thread_func,(void *)i);  
  90. if(err!=0)  
  91.         {  
  92.             printf("thread_create error:%s\n",strerror(err));  
  93.             exit(-1);  
  94.         }  
  95.     }  
  96. for (i = 0; i < NUM; i++)  
  97.     {  
  98.         err = pthread_join(tid[i], &tret);  
  99. if (err != 0)  
  100.         {  
  101.             printf("can not join with thread %d:%s\n", i,strerror(err));  
  102.             exit(-1);  
  103.         }  
  104.     }  
  105.     printf("\n");  
  106. return 0;  
  107. }  
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
//#define DEBUG 1
#define NUM 3

int n=0;
pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量
pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//條件變數


void * thread_func(void *arg)
{
    int param=(int)arg;
    char c='A'+param;
    int ret,i=0;
    for (; i < 10; i++)
    {
        pthread_mutex_lock(&mylock);
        while (param != n)  //剛執行時,n = 0, param = 0,條件不成立,所以直接列印A
        {
#ifdef DEBUG
            printf("thread %d waiting\n", param);
#endif
            ret = pthread_cond_wait(&qready, &mylock);
            if (ret == 0) 
            {
#ifdef DEBUG
                printf("thread %d wait success\n", param);
#endif
            } else 
            {
#ifdef DEBUG
                printf("thread %d wait failed:%s\n", param, strerror(ret));
#endif
            }
        }
       // printf("%d ",param+1);
        printf("%c ",c);  //列印A後
        n=(n+1)%NUM;      //n變成了1,對執行緒2會產出影響!!!!
        pthread_mutex_unlock(&mylock);
		//會喚醒所有的執行緒,因為當這個執行緒完後會等pthread_cond_wait()執行兩次後才能退出while (param != n)
        pthread_cond_broadcast(&qready);
		
    }    
    return (void *)0;
}

#if 0
//假設為執行緒2

void * thread_func(void *arg)//傳入值1
{
    int param=(int)arg;
    char c='A'+param;
    int ret,i=0;
    for (; i < 10; i++)
    {
        pthread_mutex_lock(&mylock);
        while (param != n)  //和執行緒1同時執行,所以剛開始時條件滿足
        {
#ifdef DEBUG
            printf("thread %d waiting\n", param);
#endif
			//執行到此時,等待執行緒1傳送訊號,當執行緒1的A列印完後,n的值也變成了1,條件就不成立了
            ret = pthread_cond_wait(&qready, &mylock);
            if (ret == 0) 
            {
#ifdef DEBUG
                printf("thread %d wait success\n", param);
#endif
            } else 
            {
#ifdef DEBUG
                printf("thread %d wait failed:%s\n", param, strerror(ret));
#endif
            }
        }
       // printf("%d ",param+1);
        printf("%c ",c); //此時列印值B
        n=(n+1)%NUM;    //對列印C的執行緒3產生影響!!!
        pthread_mutex_unlock(&mylock);
        pthread_cond_broadcast(&qready);
    }    
    return (void *)0;
}

#endif

int main(int argc, char** argv) {
    
    int i=0,err;
    pthread_t tid[NUM];
    void *tret;
    for(;i<NUM;i++)
    {
        err=pthread_create(&tid[i],NULL,thread_func,(void *)i);
        if(err!=0)
        {
            printf("thread_create error:%s\n",strerror(err));
            exit(-1);
        }
    }
    for (i = 0; i < NUM; i++)
    {
        err = pthread_join(tid[i], &tret);
        if (err != 0)
        {
            printf("can not join with thread %d:%s\n", i,strerror(err));
            exit(-1);
        }
    }
    printf("\n");
    return 0;
}
結果如下: