编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>
#define PRINTTIME 10
/*互斥锁 MUTEX*/
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/*条件变量*/
pthread_cond_t condA_B = PTHREAD_COND_INITIALIZER;
pthread_cond_t condB_C = PTHREAD_COND_INITIALIZER;
pthread_cond_t condC_A = PTHREAD_COND_INITIALIZER;
int flagA = 0,flagB = 0,flagC = 0;
void pthread_print(pthread_cond_t *cond_wait,char *name,int *flag_self,int *flag_wait,pthread_cond_t *cond_signal){
printf("%c come!\n",*name);
int i = 0;
while(i < PRINTTIME){
pthread_mutex_lock(&mutex);
*flag_self = 1;
pthread_cond_wait(cond_wait,&mutex);
*flag_self = 0;
printf("%c:%d-->%lx\n",*name,i,pthread_self());
while(!(*flag_wait)){
pthread_mutex_unlock(&mutex);
usleep(30);
pthread_mutex_lock(&mutex);
}/*循环结束表示flagBC为1,就是B已经加锁,并在等待A给它信号,所以现在A发信号给B*/
pthread_cond_signal(cond_signal);
pthread_mutex_unlock(&mutex);
i++;
usleep(20);
}
}
void *th_A_C(void *arg){
char *name = (char *)arg;
pthread_print(&condC_A,name,&flagA,&flagB,&condA_B);
flagA = 1;/*防止C 等待 A 而不能结束*/
}
void *th_B_A(void *arg){
char *name = (char *)arg;
pthread_print(&condA_B,name,&flagB,&flagC,&condB_C);
}
void *th_C_B(void *arg){
char *name = (char *)arg;
pthread_print(&condB_C,name,&flagC,&flagA,&condC_A);
}
int main(void){
char name1 = 'A',name2 = 'B',name3 = 'C';
char *p[] = {
(char *) &name1,
(char *) &name2,
(char *) &name3
};
void *(*th_funs[])(void *) = {
th_A_C,
th_B_A,
th_C_B
};
int count = sizeof(th_funs)/sizeof(th_funs[0]);
pthread_t th[count];
int i = 0;
while(i < count){
if(pthread_create(&th[i],NULL,th_funs[i],(void *)p[i]) < 0){
fprintf(stderr,"pthread_create th1 %s\n",strerror(errno));
exit(1);
}
usleep(30);
i++;
}
pthread_cond_broadcast(&condC_A);
for(i = 0;i < count;i++){
pthread_join(th[i],NULL);
printf("th%d finished!\n",i);
}
return 0;
}
上一个封装的不好,最后一个版本老师给的,封装的更好一些:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#define LP_TIMES 10
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_AB = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_BC = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond_CA = PTHREAD_COND_INITIALIZER;
//标记
int flag_AB,flag_BC,flag_CA;
//标记检查的线程等待
void th_condflag_wait(int *flag,pthread_cond_t *cond,pthread_mutex_t *mutex)
{
(*flag) = 1;
pthread_cond_wait(cond,mutex);
(*flag) = 0;
}
//标记检查的线程通知
void th_condflag_broadcast(int *flag,pthread_cond_t *cond,pthread_mutex_t *mutex)
{
while(!(*flag))
{
pthread_mutex_unlock(mutex);
usleep(50);
pthread_mutex_lock(mutex);
}
pthread_cond_broadcast(cond);
}
void *th_fun_A(void *arg)
{
int i = LP_TIMES;
while(i--)
{
pthread_mutex_lock(&mutex);
//A wait C
th_condflag_wait(&flag_CA,&cond_CA,&mutex);
printf("A%d: %lx\n",10-i,pthread_self());
//A cond B
th_condflag_broadcast(&flag_AB,&cond_AB,&mutex);
pthread_mutex_unlock(&mutex);
usleep(50);
}
//防止C线程最后一次等待A线程时死锁
flag_CA = 1;
}
void *th_fun_B(void *arg)
{
int i = LP_TIMES;
while(i--)
{
pthread_mutex_lock(&mutex);
//B wait A
th_condflag_wait(&flag_AB,&cond_AB,&mutex);
printf("B%d: %lx\n",10-i,pthread_self());
//B cond C
th_condflag_broadcast(&flag_BC,&cond_BC,&mutex);
pthread_mutex_unlock(&mutex);
usleep(50);
}
}
void *th_fun_C(void *arg)
{
int i = LP_TIMES;
while(i--)
{
pthread_mutex_lock(&mutex);
//C wait B
th_condflag_wait(&flag_BC,&cond_BC,&mutex);
printf("C%d: %lx\n",10-i,pthread_self());
//C cond A
th_condflag_broadcast(&flag_CA,&cond_CA,&mutex);
pthread_mutex_unlock(&mutex);
usleep(50);
}
}
int main(void)
{
printf("main: %lx\n",pthread_self());
//保存3个线程的处理函数
void *(*th_funs[])(void *) =
{
th_fun_A,
th_fun_B,
th_fun_C
};
int th_count = sizeof(th_funs)/sizeof(th_funs[0]);
pthread_t th[th_count];
int i;
for(i = 0;i < th_count;i++)
{
//通过线程函数数组记录的函数来创建线程
if(pthread_create(th+i,NULL,th_funs[i],(void *)i) < 0)
{
fprintf(stderr,"th_create: %s\n",
strerror(errno));
exit(1);
}
printf("th[%d]: %lx\n",i,th[i]);
}
//起始给A线程发出通知,防止A和C死锁
pthread_mutex_lock(&mutex);
th_condflag_broadcast(&flag_CA,&cond_CA,&mutex);
pthread_mutex_unlock(&mutex);
//回收线程
for(i = 0;i < th_count;i++)
{
pthread_join(th[i],NULL);
printf("i: %d finished!\n",i);
}
return 0;
}
原博客地址:
http://blog.csdn.net/baiding1123/article/details/14053957