4.17 作业

本文详细描述了如何使用C语言的pthread库创建ABC三个线程,通过互斥锁和条件变量控制线程执行顺序,确保打印结果为ABC的顺序。
摘要由CSDN通过智能技术生成

创建编号为ABC三个线程,三个线程循环打印自己的编号,要求打印出来的结果必须是ABC;

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
int flag = 0;

//申请锁
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;


//申请条件变量
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;




void* A(void*arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        while(0!= flag)
        {
            pthread_cond_wait(&cond,&mutex);
            if(2==flag)
                pthread_cond_signal(&cond);
        }
        printf("A--\n");
        flag = 1;
        sleep(1);
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}
                                                                                                                                         
void* B(void*arg)
{
    while(1)
    {
    //  sleep(1);
        pthread_mutex_lock(&mutex);
        while(1!= flag)
        {
            pthread_cond_wait(&cond,&mutex);
            if(0==flag)
                pthread_cond_signal(&cond);
        }
            printf("B\n");
        sleep(1);
        flag = 2;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}

void* C(void*arg)
{
    while(1)
    {
//      sleep(1);
        pthread_mutex_lock(&mutex);
        while(2!= flag)
        {
            pthread_cond_wait(&cond,&mutex);
            if(1==flag)
                pthread_cond_signal(&cond);
        }
        printf("C\n");
        sleep(1);
        flag =0;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
    pthread_exit(NULL);
}




int main(int argc, const char *argv[])
{
    //申请锁
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex,NULL);
    //申请条件变量
    pthread_cond_t cond;
    pthread_cond_init(&cond,NULL);
    //创建反正分支线程
    pthread_t tid,pid,wid;
    if(pthread_create(&tid,NULL,A,(void*)&mutex)!=0)
    {
        printf("fail %d\n",__LINE__);
        return -1;
    }
    if(pthread_create(&pid,NULL,B,(void*)&mutex)!=0)
    {
        printf("fail %d\n",__LINE__);
        return -1;
    }
    if(pthread_create(&wid,NULL,C,(void*)&mutex)!=0)
    {
        printf("fail %d\n",__LINE__);
        return -1;
    }

    pthread_join(tid,NULL);
    pthread_join(pid,NULL);
//  pthread_join(wid,NULL);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    return 0;
}
                                                                                                                                         
                                                                                                                                         

  • 23
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值