编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

方式1:

#include <stdio.h>
#include <errno.h>
#include <pthread.h>

#define MAX 10
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int flag = 1;

void *thread_A(void *arg)
{
    int i = 0;
    for(i = 0; i < MAX; i++)
    {
        pthread_mutex_lock(&mutex);
        while(1 != flag)
        {
            pthread_cond_wait(&cond, &mutex);
        }

        printf("\tthread_A_id = %u", (unsigned int)pthread_self());
        flag = 2;
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&cond);
    }

    return NULL;
}

void *thread_B(void *arg)
{
    int i = 0;
    for(i = 0; i < MAX; i++)
    {
        pthread_mutex_lock(&mutex);
        while(2 != flag)
        {
            pthread_cond_wait(&cond, &mutex);
        }

        printf("\tthread_B_id = %u", (unsigned int)pthread_self());
        flag = 3;
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&cond);
    }

    return NULL;
}

void *thread_C(void *arg)
{
    int i = 0;
    for(i = 0; i < MAX; i++)
    {
        pthread_mutex_lock(&mutex);
        while(3 != flag)
        {
            pthread_cond_wait(&cond, &mutex);
        }

        printf("\tthread_C_id = %u\n", (unsigned int)pthread_self());
        flag = 1;
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&cond);
    }

    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t th[3];
    int status;
    int i;

    status = pthread_create(&th[0], NULL, thread_A, NULL);
    if(status != 0)
    {
        printf("thread create error: %d\n", strerror(errno));
    }

    status = pthread_create(&th[1], NULL, thread_B, NULL);
    if(status != 0)
    {
        printf("thread create error: %d\n", strerror(errno));
    }

    status = pthread_create(&th[2], NULL, thread_C, NULL);
    if(status != 0)
    {
        printf("thread create error: %d\n", strerror(errno));
    }

    for(i = 0; i < 3; i++)
    {
        pthread_join(th[i], NULL);
    }
    printf("\n");

    return 0;
}

方式2:

#include <stdio.h>
#include <pthread.h>

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

#define CYCLES 10
#define THREAD_NUMS 3

int n = 0;

void *thread_fun(void *arg)
{
    int param = *(int *)arg;
    char c = 'A' + param;
    int i = 0;

    while(i++ < CYCLES)
    {
        pthread_mutex_lock(&mutex);
        while(param != n)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        printf("\tthread_%c_id=%u", c, (unsigned int)pthread_self());
        n = (n+1) % THREAD_NUMS;
        if(0 == n)
            printf("\n");
        pthread_mutex_unlock(&mutex);
        pthread_cond_broadcast(&cond);
    }

    return NULL;
}

int main(int argc, char*argv[])
{
    int i = 0;
    int arr[THREAD_NUMS];
    for(i = 0; i < THREAD_NUMS; i++)
    {
        arr[i] = i;
    }
    pthread_t thread_arr[THREAD_NUMS];

    for(i = 0; i < THREAD_NUMS; i++)
    {
        int status = pthread_create(&thread_arr[i], NULL, thread_fun, (void *)&arr[i]);
        if(status != 0)
            printf("thread create error!\n");
    }

    for(i = 0; i < THREAD_NUMS; i++)
    {
        pthread_join(thread_arr[i], NULL);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值