调用pthread_cond_wait为2个线程 [英] pthread_cond_wait for 2 threads

查看:105
本文介绍了调用pthread_cond_wait为2个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施调用pthread_cond_wait 2线程。我的测试code正试图用两个线程preform以下情形:

I'm trying to implement pthread_cond_wait for 2 threads. My test code is trying to use two threads to preform the following scenario:


  • 线程B等待状态

  • 线程A打印Hello五次

  • 线程A信号线程B

  • 线程A等待

  • 线程B打印再见

  • 线程B信号线程A

  • 循环开始(X5)

到目前为止,code打印Hello五次,然后卡住。从例子我看着好像我是在正确的轨道上锁定互斥,等待,得到由其他线程发出信号,解除互斥体,做的东西,循环

So far the code prints "Hello" five times and then gets stuck. From examples I've looked at it seems I'm on the right track, "Lock mutex, wait, get signaled by other thread, unlock mutex, do stuff, loop"

测试code:

//Import 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

//global variables
pthread_cond_t      condA  = PTHREAD_COND_INITIALIZER;
pthread_cond_t      condB  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;




void *threadA()
{
    int i = 0, rValue, loopNum;

    while(i<5)
    {
        //unlock mutex
        rValue = pthread_mutex_unlock(&mutex);

        //do stuff
        for(loopNum = 1; loopNum <= 5; loopNum++)
            printf("Hello %d\n", loopNum);

        //signal condition of thread b
        rValue = pthread_cond_signal(&condB);

        //lock mutex
        rValue = pthread_mutex_lock(&mutex);

        //wait for turn
        while( pthread_cond_wait(&condA, &mutex) != 0 )

        i++;
    }

}



void *threadB()
{
    int n = 0, rValue;

    while(n<5)
    {
        //lock mutex
        rValue = pthread_mutex_lock(&mutex);

        //wait for turn
        while( pthread_cond_wait(&condB, &mutex) != 0 )

        //unlock mutex
        rValue = pthread_mutex_unlock(&mutex);

        //do stuff
        printf("Goodbye");

        //signal condition a
        rValue = pthread_cond_signal(&condA);

        n++;        
    }
}




int main(int argc, char *argv[])
{
    //create our threads
    pthread_t a, b;

    pthread_create(&a, NULL, threadA, NULL);
    pthread_create(&b, NULL, threadB, NULL);

    pthread_join(a, NULL);
    pthread_join(b,NULL);
}

在正确的方向指针会大大AP preciated,谢谢!
(在Linux上使用编译code的gcc -o timeTest.c -lpthread timeTest)

A pointer in the right direction would be greatly appreciated, thanks! (Code compiled on Linux using "gcc timeTest.c -o timeTest -lpthread")

推荐答案

您有两个问题。第一个是你不使用而()回路正确的 - 例如,在这里:

You have two problems. The first is that you aren't using while() loops correctly - for example, here:

//wait for turn
while( pthread_cond_wait(&condA, &mutex) != 0 )

i++;

,而循环体是语句我++ - 这将执行调用pthread_cond_wait ()我++ 直到调用pthread_cond_wait()返回一个错误,所以这基本上是一个死循环。

The body of the while loop is the statement i++ - this will execute pthread_cond_wait() and i++ until the pthread_cond_wait() returns an error, so this is essentially an endless loop.

第二个是,你不能对自己使用的pthreads条件变量 - 它需要用一些实际的共​​享状态配对(简单地说,这个共享的状态可能只是由一个互斥保护标志变量)。在调用pthread_cond_wait()函数用于等待共享状态达到一定值,调用pthread_cond_signal()当一个线程改变了共享的状态下使用的功能。返工您的示例使用了这样的变量:

The second is that you can't use a pthreads condition variable on its own - it needs to be paired with some actual shared state (at its simplest, this shared state might just be a flag variable protected by a mutex). The pthread_cond_wait() function is used to wait for the shared state to reach a certain value, and the pthread_cond_signal() function is used when a thread has altered the shared state. Reworking your example to use such a variable:

//global variables
/* STATE_A = THREAD A runs next, STATE_B = THREAD B runs next */
enum { STATE_A, STATE_B } state = STATE_A;
pthread_cond_t      condA  = PTHREAD_COND_INITIALIZER;
pthread_cond_t      condB  = PTHREAD_COND_INITIALIZER;
pthread_mutex_t     mutex = PTHREAD_MUTEX_INITIALIZER;

void *threadA()
{
    int i = 0, rValue, loopNum;

    while(i<5)
    {
        /* Wait for state A */
        pthread_mutex_lock(&mutex);
        while (state != STATE_A)
            pthread_cond_wait(&condA, &mutex);
        pthread_mutex_unlock(&mutex);

        //do stuff
        for(loopNum = 1; loopNum <= 5; loopNum++)
            printf("Hello %d\n", loopNum);

        /* Set state to B and wake up thread B */
        pthread_mutex_lock(&mutex);
        state = STATE_B;
        pthread_cond_signal(&condB);
        pthread_mutex_unlock(&mutex);

        i++;
    }

    return 0;
}

void *threadB()
{
    int n = 0, rValue;

    while(n<5)
    {
        /* Wait for state B */
        pthread_mutex_lock(&mutex);
        while (state != STATE_B)
            pthread_cond_wait(&condB, &mutex);
        pthread_mutex_unlock(&mutex);

        //do stuff
        printf("Goodbye\n");

        /* Set state to A and wake up thread A */
        pthread_mutex_lock(&mutex);
        state = STATE_A;
        pthread_cond_signal(&condA);
        pthread_mutex_unlock(&mutex);

        n++;
    }

    return 0;
}

需要注意的是使用两个条件变量康达 condB 是不必要在这里 - 在code会如果使用条件只有一个变量,而不是只是为正确的。

Note that the use of two condition variables condA and condB is unnecessary here - the code would be just as correct if only one condition variable was used instead.

这篇关于调用pthread_cond_wait为2个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆