具有互斥锁演示程序的条件变量挂起

时间:2013-01-14 08:02:06

标签: c++ pthreads mutex

想法:

当线程A需要检查条件变量x的状态时,它将首先保持互斥锁,然后检查变量的状态,如果发现无效,它将开始等待。

由于线程A当前具有互斥锁,因此线程B不会中断。一旦线程A等待,它将释放互斥锁。
然后,线程B可以获取互斥锁,并做任何它想做的事情。

#include <stdlib.h>
#include <pthread.h>
#include <iostream>

pthread_mutex_t mutexLock  = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  cond       = PTHREAD_COND_INITIALIZER;
void *                functionOfThreadA (void * ptr);
void *                functionOfThreadB (void * ptr);
int                     x  = 0;

int main ()
{
    pthread_t threadA;
    pthread_t threadB;
    char      *messageA = (char *) "Thread A"; // TODO
    char      *messageB = (char *) "Thread B";
    int       returnValueA;
    int       returnValueB;


    returnValueA = pthread_create (&threadA, NULL, functionOfThreadA, (void*) messageA);
    returnValueB = pthread_create (&threadB, NULL, functionOfThreadB, (void*) messageB);

    pthread_join (threadA, NULL);
    pthread_join (threadB, NULL);

    exit (0);
}

void * functionOfThreadA (void * ptr)
{
    char * message;
    message = (char *) ptr;
    std :: cout << "\nA: " << message << "\n";

    while (1)
    {
        pthread_mutex_lock (&mutexLock);

        if (x == 10)
        {
            std :: cout << "\nx == 10 : true, functionOfThread A: " << message << "\n";
        }
        else
        {
            std :: cout << "\nThread waits." << "\n";
            pthread_cond_wait (&cond, &mutexLock);
            std :: cout << "\nA:++ " << message << "\n";
        }

        pthread_mutex_unlock (&mutexLock);
        return 0;
    }
}

void * functionOfThreadB (void * ptr)
{
    while (1)
    {
        char * message;
        message = (char *) ptr;

        pthread_mutex_lock (&mutexLock);

        x = x + 1;
        if (x == 10)
        {
            std :: cout << "\nfunctionOfThread B: " << message << "\n";
            pthread_cond_signal (&cond);
        }
        else
        {
            std :: cout << "\nB: Not signaled yet. Value of x: " << x << "\n";
        }

        pthread_mutex_unlock (&mutexLock);
        return 0;
    }
}

输出:

anisha@linux-trra:~> ./a.out 
B: Not signaled yet. Value of x: 1

A: Thread A

Thread waits.
^C

然后它就会挂起。

我需要在此更正以及为什么?

2 个答案:

答案 0 :(得分:1)

线程在此等待pthread_cond_wait (&cond, &mutexLock);,因为没有其他线程可以发出信号。线程B对锁和条件变量

没有任何作用

答案 1 :(得分:0)

已发现问题。

发出信号后,我需要退出循环。但在我这样做之前,我还需要解锁互斥锁。 functionOfThreadB中添加的代码已在下面的程序中突出显示为 * ** * **

#include <stdlib.h>
#include <pthread.h>
#include <iostream>

pthread_mutex_t mutexLock  = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  cond       = PTHREAD_COND_INITIALIZER;
void *                functionOfThreadA (void * ptr);
void *                functionOfThreadB (void * ptr);
int                     x  = 0;

int main ()
{
    pthread_t threadA;
    pthread_t threadB;
    char      *messageA = (char *) "Thread A";
    char      *messageB = (char *) "Thread B";
    int       returnValueA;
    int       returnValueB;

    returnValueA = pthread_create (&threadA, NULL, functionOfThreadA, (void*) messageA);
    returnValueB = pthread_create (&threadB, NULL, functionOfThreadB, (void*) messageB);

    pthread_join (threadA, NULL);
    pthread_join (threadB, NULL);

    exit (0);
}

void * functionOfThreadA (void * ptr)
{
    char * message;
    message = (char *) ptr;

    while (1)
    {
        pthread_mutex_lock (&mutexLock);

        if (x == 1000)
        {
            std :: cout << "\nA: x is now 1000. Waiting period over."<< "\n";
            return 0;
        }
        else
        {
            std :: cout << "\nNow, thread A will wait for value of `x` to reach 1000" << "\n";
            pthread_cond_wait (&cond, &mutexLock);
        }

        pthread_mutex_unlock (&mutexLock);
    }
}

void * functionOfThreadB (void * ptr)
{
    while (1)
    {
        char * message;
        message = (char *) ptr;

        pthread_mutex_lock (&mutexLock);

        x = x + 1;
        if (x == 1000)
        {
            std :: cout << "\nB: Signaled. Value of x: " << x << "\n";
            pthread_cond_signal (&cond);
    *************pthread_mutex_unlock (&mutexLock);*************
    *************return 0;*************
        }
        else
        {
            std :: cout << "\nB: Not signaled yet. Value of x: " << x << "\n";
        }

        pthread_mutex_unlock (&mutexLock);
    }
}
相关问题