線程順序打印ABC

編寫一個程序,開啓3個線程,這3個線程的ID分別爲ABC,每個線程將自己的ID在屏幕上打印10遍,要求輸出結果必須按ABC的順序顯示;如:ABCABC.依次遞推。


程序代碼如下:

#include<iostream>
#include<cstdlib>
using namespace std;


#ifdef __cplusplus
extern "C"
{
#endif


#include<pthread.h>
#include<semaphore.h>
#ifdef __cplusplus
}
#endif


sem_t sem1,sem2,sem3;
int var = 1;
void *PrintA(void *ptr)
{
    int i;
    for(i = 0; i < 10; i++)
    {
         sem_wait(&sem1);
         cout<<"A";
         sem_post(&sem2);
   } 
   return NULL;
}
void *PrintB(void *ptr)
{
    for(int i = 0; i < 10; i++)
    {
         
         sem_wait(&sem2);
         cout<<"B";
         sem_post(&sem3);
    }
    return NULL;
}
void *PrintC(void *ptr)
{
    for(int i = 0; i < 10; i++)
    {
         sem_wait(&sem3);
         cout<<"C";
         sem_post(&sem1);
    }
    return NULL;
}




void init_thread()
{
   int rc1,rc2,rc3;
   pthread_t thread1,thread2,thread3;
   sem_init(&sem1,0,0);
   sem_init(&sem2,0,0);
   sem_init(&sem3,0,0);
   
   if( (rc1 = pthread_create(&thread1,NULL,PrintA,NULL) ))
   {
       cout<<"create thread is failed"<<endl;
       exit(1);
   }
   
   if( (rc2 = pthread_create(&thread2,NULL,PrintB,NULL) ))
   {
       cout<<"create thread is failed"<<endl;
       exit(1);
   }
   
   if( (rc3 = pthread_create(&thread3,NULL,PrintC,NULL) ))
   {
       cout<<"create thread is failed"<<endl;
       exit(1);
   }
   sem_post(&sem1);
   pthread_join(thread1,NULL);
   pthread_join(thread2,NULL);
   pthread_join(thread3,NULL);
}
int main(int argc,char *argv[])
{
   init_thread();
   cout<<endl;   
   return 0;
}

 

 

 

方法二

 

  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <pthread.h>
 
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  int k = 0;
 
  void* rountine_A(void* arg)
  {
   int i;
   char ch = (int)arg;
   pthread_detach(pthread_self());
   for(i=0;i<10;++i) {
     //printf("A1 thread is going\n");
     pthread_mutex_lock(&mutex);
     //printf("A2 thread is going\n");
     while('A'==ch && k!=0) {
         //printf("A3 thread is going\n");
        pthread_cond_wait(&cond,&mutex);
         //printf("A4 thread is going\n");
   }
     printf("%c\n",ch);
     fflush(stdout);
     k = (k+1)%3;
     pthread_cond_broadcast(&cond);
     pthread_mutex_unlock(&mutex);
    }
   return (void*)0; 
 }

  void* rountine_B(void* arg)
  {
   int i;
   char ch = (int)arg;
   pthread_detach(pthread_self());
   for(i=0;i<10;++i) {
     //printf("B1 thread is going\n");
     pthread_mutex_lock(&mutex);
     //printf("B2 thread is going\n");
     while('B'==ch && k!=1)  {
        //printf("B3 thread is going\n");
        pthread_cond_wait(&cond,&mutex);
        //printf("B4 thread is going\n");
       }
     printf("%c\n",ch);
     fflush(stdout);
     k = (k+1)%3;
     pthread_cond_broadcast(&cond);
     pthread_mutex_unlock(&mutex);
    }
   return (void*)0; 
 }


void* rountine_C(void* arg)
  {
   int i;
   char ch = (int)arg;
   pthread_detach(pthread_self());
   for(i=0;i<10;++i) {
     //printf("C1 thread is going\n");
     pthread_mutex_lock(&mutex);
     //printf("C2 thread is going\n");
     while('C'==ch && k!=2) {
        //printf("C3 thread is going\n");
        pthread_cond_wait(&cond,&mutex);
        //printf("C4 thread is going\n");
       }
     printf("%c\n",ch);
     fflush(stdout);
     k = (k+1)%3;
     pthread_cond_broadcast(&cond);
     pthread_mutex_unlock(&mutex);
    }
   return (void*)0; 
 }


   int main(void)
   {
   pthread_t tid[3];
   pthread_create(&tid[0],NULL,rountine_A,(void*)('A'));
   pthread_create(&tid[1],NULL,rountine_B,(void*)('B'));
   pthread_create(&tid[2],NULL,rountine_C,(void*)('C'));
   sleep(1);
   printf("\n");
   return 0; 
   }

 

發佈了28 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章