僵局的原因在哪里?

发布于 2024-12-12 06:25:39 字数 1745 浏览 0 评论 0原文

我打算让两个线程等待来自第三个线程的信号。

这两个线程执行相同的工作,但一次只有一个线程获取信号。一旦满足特定条件(捕获的信号数量),它们就会自行终止。

然后最终主线程取消了第三个线程。

我遇到了僵局,但无法弄清楚问题出在哪里。

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int n = 0;
int count = 0;

void* func1(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for func3 to signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate func1\n", n);
            return (NULL);
        }
        else
        {
            printf("func1 got number:%d\n", n);
        }
        pthread_mutex_unlock(&mutex);
    }
}

void* func2(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for func3 to signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate func2\n", n);
            return (NULL);
        }
        else
        {
            printf("func2 got number:%d\n", n);
        }
        pthread_mutex_unlock(&mutex);
    }
}    

void* func3(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        n++;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
}


int main(int argc, char *argv[])
{
    pthread_t t1, t2, t3;

    pthread_create(&t1, NULL, func1, NULL);
    pthread_create(&t2, NULL, func2, NULL);
    pthread_create(&t3, NULL, func3, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    pthread_cancel(t3);

    return 0;
}

I intent to have two threads waiting for signals from a third thread.

These two threads do the same work but only one of them gets the signal at a time. Once a certain condition is meet(number of signal captured), they terminate themselves.

Then in the end the main thread cancel the third thread.

I got deadlock, but could not figure out where is the problem.

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

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int n = 0;
int count = 0;

void* func1(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for func3 to signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate func1\n", n);
            return (NULL);
        }
        else
        {
            printf("func1 got number:%d\n", n);
        }
        pthread_mutex_unlock(&mutex);
    }
}

void* func2(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);

        // wait for func3 to signal
        pthread_cond_wait(&cond, &mutex);
        count++;

        if(count > 10)
        {
            printf("number %d terminate func2\n", n);
            return (NULL);
        }
        else
        {
            printf("func2 got number:%d\n", n);
        }
        pthread_mutex_unlock(&mutex);
    }
}    

void* func3(void *ptr)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        n++;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
    }
}


int main(int argc, char *argv[])
{
    pthread_t t1, t2, t3;

    pthread_create(&t1, NULL, func1, NULL);
    pthread_create(&t2, NULL, func2, NULL);
    pthread_create(&t3, NULL, func3, NULL);

    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    pthread_cancel(t3);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2024-12-19 06:25:39

func1func2 退出时,您并未解锁互斥锁。

You're not unlocking the mutex when func1 or func2 exits.

淡忘如思 2024-12-19 06:25:39

我认为问题是(正如 pst 指出的)是 return (NULL); 在您的 func1func2 函数中;因为 pthread_cond_wait(3posix) 返回时互斥体已锁定,因此当它们退出时,互斥体将保持锁定状态:

   These functions atomically release mutex and cause the
   calling thread to block on the condition variable cond;
   ...
   Upon successful return, the mutex shall have been locked and
   shall be owned by the calling thread.

尝试在 return (NULL); 之前解锁互斥体。

I think the problem is (as pst points out) is the return (NULL); in your func1 and func2 functions; because pthread_cond_wait(3posix) returns with the mutex locked, when they exit, the mutex is left locked:

   These functions atomically release mutex and cause the
   calling thread to block on the condition variable cond;
   ...
   Upon successful return, the mutex shall have been locked and
   shall be owned by the calling thread.

Try unlocking your mutex before return (NULL);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文