linux多线程交替打印ABC

1.POSIX实现多线程打印ABC

  1 #include<pthread.h>
  2 #include<iostream>
  3 using namespace std;
  4 
  5 static pthread_mutex_t mtx1=PTHREAD_MUTEX_INITIALIZER;
  6 static pthread_mutex_t mtx2=PTHREAD_MUTEX_INITIALIZER;
  7 static pthread_mutex_t mtx3=PTHREAD_MUTEX_INITIALIZER;
  8 static pthread_cond_t conda=PTHREAD_COND_INITIALIZER;
  9 static pthread_cond_t condb=PTHREAD_COND_INITIALIZER;
 10 static pthread_cond_t condc=PTHREAD_COND_INITIALIZER;
 11 void * functionA(void*){
 12         int i=10;
 13         while(i--){
 14                 pthread_mutex_lock(&mtx1);
 15                 pthread_cond_wait(&condc,&mtx1);
 16                 cout<<"A"<<endl;
 17                 pthread_mutex_unlock(&mtx1);
 18                 pthread_cond_signal(&conda);
 19         }
 20 }

void * functionB(void*){
 22         int i=10;
 23         while(i--){
 24                 pthread_mutex_lock(&mtx2);
 25                 pthread_cond_wait(&conda,&mtx2);
 26                 cout<<"B"<<endl;
 27                 pthread_mutex_unlock(&mtx2);
 28                 pthread_cond_signal(&condb);
 29         }       
 30 }
 31 void * functionC(void*){
 32         int i=10;
 33         while(i--){
 34                 pthread_mutex_lock(&mtx3);
 35                 pthread_cond_wait(&condb,&mtx3);
 36                 cout<<"C"<<endl;
 37                 pthread_mutex_unlock(&mtx3);
 38                 pthread_cond_signal(&condc);
 39         }
 40 }
 41 
 42 int main(){
 43         pthread_t id1,id2,id3;
 44         pthread_create(&id1,NULL,functionA,NULL);
 45         pthread_create(&id2,NULL,functionB,NULL);
 46         pthread_create(&id3,NULL,functionC,NULL);
 47         pthread_cond_signal(&condc);
 48         pthread_join(id1,NULL);
 49         pthread_join(id2,NULL);
 50         pthread_join(id3,NULL);
 51         return 0;
 52 }

2.封装类实现

2.1 locker头文件实现

  1 #ifndef LOCKER_H
  2 #define LOCKER_H
  3 
  4 #include<pthread.h>
  5 #include<exception>
  6 #include<semaphore.h>
  7 
  8 class sem{
  9 public:
 10         sem(){
 11                 if(sem_init(&m_sem,0,0)!=0)
 12                         throw std::exception();
 13         }
 14         sem(int num){
 15                 if(sem_init(&m_sem,0,num)!=0)
 16                         throw std::exception();
 17         }
 18         ~sem(){
 19                 sem_destroy(&m_sem);
 20         }
 21         bool wait(){
 22                 return sem_wait(&m_sem)==0;
 23         }
 24         bool post(){
 25                 return sem_post(&m_sem)==0;
 26         }
 27 
 28 private:
 29         sem_t m_sem;
 30 
 31 };
class locker{
 34 public:
 35         locker(){
 36                 if(pthread_mutex_init(&m_mutex,NULL)!=0)
 37                         throw std::exception();
 38         }
 39         ~locker(){
 40                 pthread_mutex_destroy(&m_mutex);
 41         }
 42         bool lock(){
 43                 return pthread_mutex_lock(&m_mutex)==0;
 44         }
 45         bool unlock(){
 46                 return pthread_mutex_unlock(&m_mutex)==0;
 47         }
 48         pthread_mutex_t* get(){
 49                 return &m_mutex;
 50         }
 51 
 52 private:
 53         pthread_mutex_t m_mutex;
 54 };
 55 
 56 class cond{
 57 public:
 58         cond(){
 59                 if(pthread_mutex_init(&m_mutex,NULL)!=0)
 60                         throw std::exception();
 61                 if(pthread_cond_init(&m_cond,NULL)!=0){
 62                         pthread_mutex_destroy(&m_mutex);
 63                         throw std::exception();
 64                 }
 65 
 66         }
 67         ~cond(){
 68                 pthread_mutex_destroy(&m_mutex);
 69                 pthread_cond_destroy(&m_cond);
 70         }
 71         bool wait(){
 72                 int ret=0;
 73                 pthread_mutex_lock(&m_mutex);
 74                 ret=pthread_cond_wait(&m_cond,&m_mutex);
 75                 pthread_mutex_unlock(&m_mutex);
 76                 return ret==0;
 77         }
 78         bool signal(){
 79                 return pthread_cond_signal(&m_cond)==0;
 80         }
 81 
 82 private:
 83         pthread_mutex_t m_mutex;
 84         pthread_cond_t m_cond;
 85 };
 86 
 87 #endif

2.2 多线程打印ABC

  1 #include<pthread.h>
  2 #include<iostream>
  3 #include "locker.h"
  4 using namespace std;
  5 cond a_cond,b_cond,c_cond;
  6 void * functionA(void*){
  7         int i=10;
  8         while(i--){
  9                 c_cond.wait();
 10                 cout<<"A"<<endl;
 11                 a_cond.signal();
 12         }
 13 }
 14 void * functionB(void*){
 15         int i=10;
 16         while(i--){
 17                 a_cond.wait();
 18                 cout<<"B"<<endl;
 19                 b_cond.signal();
 20 
 21         }
 22 }
 23 void * functionC(void*){
 24         int i=10;
 25         while(i--){
 26                 b_cond.wait();
 27                 cout<<"C"<<endl;
 28                 c_cond.signal();
 29         }
 30 }
 31 
 32 int main(){
 33         pthread_t id1,id2,id3;
 34         pthread_create(&id1,NULL,functionA,NULL);
 35         pthread_create(&id2,NULL,functionB,NULL);
 36         pthread_create(&id3,NULL,functionC,NULL);
 37         c_cond.signal();
 38         pthread_join(id1,NULL);
 39         pthread_join(id2,NULL);
 40         pthread_join(id3,NULL);
 41         return 0;
 42 }

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值