0

主意:

当线程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

然后它只是挂起。

我需要在这里更正什么,为什么?

4

2 回答 2

1

线程在这里 pthread_cond_wait (&cond, &mutexLock);无限等待,因为没有其他线程向它发出信号。线程 B 对锁和条件变量不做任何事情

于 2013-01-14T08:04:14.233 回答
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);
    }
}
于 2013-01-14T11:11:52.393 回答