题目描述:
三个线程打印1-100
- 线程1打印1,4,7…
- 线程2打印2,5,8…
- 线程3打印3,6,9…
实现一:互斥锁+条件变量
代码
//tes2.cpp
#include <pthread.h>
#include <iostream>
#include <unistd.h>
using namespace std;
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
int cnt=1;
void* fun(void* k){ //flag = 0,1,2
int flag = *(int*)(k);
cout<<"this is thread :"<<flag+1<<endl;
while(cnt<=100){
pthread_mutex_lock(&m_mutex);
while((cnt-1)%3 != flag)
pthread_cond_wait(&m_cond, &m_mutex);
// (cnt-1)%3 == flag
cout<<"thread "<<flag+1<<" :"<<cnt++<<endl;
if(cnt>100)
return((void *)0);
pthread_mutex_unlock(&m_mutex);
pthread_cond_broadcast(&m_cond);
}
return((void *)0);
}
int main(){
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
pthread_t m_thread[3];
int arg[3]={0,1,2};
for(int i=0; i<3;++i)
pthread_create(&m_thread[i], NULL, fun, &arg[i]);
sleep(5);
return 0;
}
编译运行
g++ test2.cpp -lpthread -o test2 && ./test2
输出
this is thread :1
thread 1 :1
this is thread :2
thread 2 :2
this is thread :3
thread 3 :3
thread 1 :4
thread 2 :5
thread 3 :6
thread 1 :7
thread 2 :8
thread 3 :9
thread 1 :10
thread 2 :11
thread 3 :12
thread 1 :13
thread 2 :14
thread 3 :15
thread 1 :16
thread 2 :17
thread 3 :18
thread 1 :19
thread 2 :20
thread 3 :21
thread 1 :22
thread 2 :23
thread 3 :24
thread 1 :25
thread 2 :26
thread 3 :27
thread 1 :28
thread 2 :29
thread 3 :30
thread 1 :31
thread 2 :32
thread 3 :33
thread 1 :34
thread 2 :35
thread 3 :36
thread 1 :37
thread 2 :38
thread 3 :39
thread 1 :40
thread 2 :41
thread 3 :42
thread 1 :43
thread 2 :44
thread 3 :45
thread 1 :46
thread 2 :47
thread 3 :48
thread 1 :49
thread 2 :50
thread 3 :51
thread 1 :52
thread 2 :53
thread 3 :54
thread 1 :55
thread 2 :56
thread 3 :57
thread 1 :58
thread 2 :59
thread 3 :60
thread 1 :61
thread 2 :62
thread 3 :63
thread 1 :64
thread 2 :65
thread 3 :66
thread 1 :67
thread 2 :68
thread 3 :69
thread 1 :70
thread 2 :71
thread 3 :72
thread 1 :73
thread 2 :74
thread 3 :75
thread 1 :76
thread 2 :77
thread 3 :78
thread 1 :79
thread 2 :80
thread 3 :81
thread 1 :82
thread 2 :83
thread 3 :84
thread 1 :85
thread 2 :86
thread 3 :87
thread 1 :88
thread 2 :89
thread 3 :90
thread 1 :91
thread 2 :92
thread 3 :93
thread 1 :94
thread 2 :95
thread 3 :96
thread 1 :97
thread 2 :98
thread 3 :99
thread 1 :100
实现二:信号量
//test1.cpp
#include <semaphore.h>
#include <iostream>
#include <unistd.h>
using namespace std;
sem_t sem1,sem2,sem3;
int cnt=1;
void* fun1(void* arg){
cout<<"this is thread 1:"<<endl;
while(true){
sem_wait(&sem1);
if(cnt>100)
break;
cout<<"thread 1 :"<<cnt++<<endl;
sem_post(&sem2);
}
return((void *)0);
}
void* fun2(void* arg){
cout<<"this is thread 2:"<<endl;
while(true){
sem_wait(&sem2);
if(cnt>100)
break;
cout<<"thread 2 :"<<cnt++<<endl;
sem_post(&sem3);
}
return((void *)0);
}
void* fun3(void* arg){
cout<<"this is thread 3:"<<endl;
while(true){
sem_wait(&sem3);
if(cnt>100)
break;
cout<<"thread 3 :"<<cnt++<<endl;
sem_post(&sem1);
}
return((void *)0);
}
int main(){
sem_init(&sem1, 0, 1);
sem_init(&sem2, 0, 0);
sem_init(&sem3, 0, 0);
pthread_t m_thread[3];
pthread_create(&m_thread[0], NULL, fun1, NULL);
pthread_create(&m_thread[1], NULL, fun2, NULL);
pthread_create(&m_thread[2], NULL, fun3, NULL);
sleep(5);
return 0;
}
编译运行
g++ test1.cpp -lpthread -o test1 && ./test1
输出:
this is thread 1:
thread 1 :1
this is thread 2:
thread 2 :2
this is thread 3:
thread 3 :3
thread 1 :4
thread 2 :5
thread 3 :6
thread 1 :7
thread 2 :8
thread 3 :9
thread 1 :10
thread 2 :11
thread 3 :12
thread 1 :13
thread 2 :14
thread 3 :15
thread 1 :16
thread 2 :17
thread 3 :18
thread 1 :19
thread 2 :20
thread 3 :21
thread 1 :22
thread 2 :23
thread 3 :24
thread 1 :25
thread 2 :26
thread 3 :27
thread 1 :28
thread 2 :29
thread 3 :30
thread 1 :31
thread 2 :32
thread 3 :33
thread 1 :34
thread 2 :35
thread 3 :36
thread 1 :37
thread 2 :38
thread 3 :39
thread 1 :40
thread 2 :41
thread 3 :42
thread 1 :43
thread 2 :44
thread 3 :45
thread 1 :46
thread 2 :47
thread 3 :48
thread 1 :49
thread 2 :50
thread 3 :51
thread 1 :52
thread 2 :53
thread 3 :54
thread 1 :55
thread 2 :56
thread 3 :57
thread 1 :58
thread 2 :59
thread 3 :60
thread 1 :61
thread 2 :62
thread 3 :63
thread 1 :64
thread 2 :65
thread 3 :66
thread 1 :67
thread 2 :68
thread 3 :69
thread 1 :70
thread 2 :71
thread 3 :72
thread 1 :73
thread 2 :74
thread 3 :75
thread 1 :76
thread 2 :77
thread 3 :78
thread 1 :79
thread 2 :80
thread 3 :81
thread 1 :82
thread 2 :83
thread 3 :84
thread 1 :85
thread 2 :86
thread 3 :87
thread 1 :88
thread 2 :89
thread 3 :90
thread 1 :91
thread 2 :92
thread 3 :93
thread 1 :94
thread 2 :95
thread 3 :96
thread 1 :97
thread 2 :98
thread 3 :99
thread 1 :100