智慧 + 毅力 = 无所不能

正确性、健壮性、可靠性、效率、易用性、可读性、可复用性、兼容性、可移植性...

导航

迅雷的笔试题

Posted on 2015-06-15 10:00  Bill Yuan  阅读(1457)  评论(0编辑  收藏  举报

转自:http://blog.csdn.net/baiding1123/article/details/14053957

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

互斥锁和条件变量。

假设有三个线程A, B, C;A线程首先执行,B线程等待A结束后执行,C线程等待B结束后执行,A线程又要等待C线程结束后执行,B再等待A,C再等待B....以此类推,是一个循环等待的过程,每一个线程要做的事情:1.抢到锁,设置标志位为1(告诉上家自己在等待信号);2.收到上家的信号,立即设置标志为为0,并打印出自己的线程号;3.确定下家是否在等待,确定下家在等待后,立即发送信号给下家,再打开锁,usleep(20)(让其他线程抢到锁)。

其中有两个要注意的地方:1.三个线程创建好后,三个线程都在死等待 [A等C,B等A,C等B] 所以首先手动代替C为A发送信号,让程序执行。2.A线程的10次循环结束后,flag标志位是0,而此时C线程还没有执行完毕,C要检查A的标志为是否为1再给A发送信号,所以在A循环结束后把它的flag标志为置为1。

 
#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;
}

</pthread></errno></string></stdlib></stdio>