为什么使用PTHREAD_COND_WAIT和PTHREAD_COND_SIGNAL时不会进行线程同步,在100个周期中失败了30次

发布于 2025-02-07 05:10:52 字数 2020 浏览 1 评论 0原文

#include <iostream>
#include <thread>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
pthread_mutex_t mutexVar = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutexSync = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condVar = PTHREAD_COND_INITIALIZER;


void startReceiving()
{
    while(1)
    {
        pthread_mutex_lock(&mutexVar);
        cout<<"lock mutex and waiting for cond_signal"<<endl;
        pthread_cond_wait(&condVar, &mutexVar);
        pthread_mutex_unlock(&mutexVar);
        
        cout<<"Received cond_signal"<<endl;
        for(int i=0;i<10;i++)
        {
            cout<<i<<" ";
        }
        cout<<endl;       
    }
}

void func1()
{
    pthread_mutex_lock(&mutexSync);
    cout<<"calling cond_signal func1"<<endl;
    pthread_cond_signal(&condVar);
    pthread_mutex_unlock(&mutexSync);
}

void func2()
{
    pthread_mutex_lock(&mutexSync);
    cout<<"calling cond_signal func2"<<endl;
    pthread_cond_signal(&condVar);
    pthread_mutex_unlock(&mutexSync);
}

void func3()
{
    pthread_mutex_lock(&mutexSync);
    cout<<"calling cond_signal func3"<<endl;
    pthread_cond_signal(&condVar);
    pthread_mutex_unlock(&mutexSync);
}
int main()
{
    thread t1(startReceiving);
    cout<<"started startReceiving and waiting"<<endl;
    sleep(2);
    thread t2(func1);
    thread t3(func2);
    thread t4(func3);
    
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    return 0;
}

我观察到的是,startreceiving()需要更多的时间来执行与其他功能相比,因此,当我们从func1()发送cond_signal,而在time startreceiviving()完成执行时,func2(func2()也会发送cond_signal和cond_wait缺少cond_wait和cond_wait。第二个信号是因为Startreceiving()尚未准备好接收cond_signal。我的理解是对吗?

我试图改进这一点的一种方法是在cond_signal之后在每个弹性调用中添加sleep(),但不确定这是否起作用。因为在10个周期中面对3次问题。由于这个问题,我缺少Startreceiving()中的数据,

因此如何改进此代码相对于同步或我在此处缺少的其他内容。

#include <iostream>
#include <thread>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
pthread_mutex_t mutexVar = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutexSync = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condVar = PTHREAD_COND_INITIALIZER;


void startReceiving()
{
    while(1)
    {
        pthread_mutex_lock(&mutexVar);
        cout<<"lock mutex and waiting for cond_signal"<<endl;
        pthread_cond_wait(&condVar, &mutexVar);
        pthread_mutex_unlock(&mutexVar);
        
        cout<<"Received cond_signal"<<endl;
        for(int i=0;i<10;i++)
        {
            cout<<i<<" ";
        }
        cout<<endl;       
    }
}

void func1()
{
    pthread_mutex_lock(&mutexSync);
    cout<<"calling cond_signal func1"<<endl;
    pthread_cond_signal(&condVar);
    pthread_mutex_unlock(&mutexSync);
}

void func2()
{
    pthread_mutex_lock(&mutexSync);
    cout<<"calling cond_signal func2"<<endl;
    pthread_cond_signal(&condVar);
    pthread_mutex_unlock(&mutexSync);
}

void func3()
{
    pthread_mutex_lock(&mutexSync);
    cout<<"calling cond_signal func3"<<endl;
    pthread_cond_signal(&condVar);
    pthread_mutex_unlock(&mutexSync);
}
int main()
{
    thread t1(startReceiving);
    cout<<"started startReceiving and waiting"<<endl;
    sleep(2);
    thread t2(func1);
    thread t3(func2);
    thread t4(func3);
    
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    return 0;
}

What i observed is, startReceiving() requires more time to execute compare to other functions, so when we send cond_signal from func1() and by the time startReceiving() completes it's execution, func2() will also send the cond_signal and cond_wait is missing that 2nd signal because startReceiving() is not yet ready to receive the cond_signal. Is my understanding right??

One way i tried to improve this is by adding sleep() inside every func call after cond_signal, but not sure this will work or not. Because facing the issue 3 times in 10 cycles. Because of this issue, i am missing the data in startReceiving()

So how to improve this code with respect to sync or anything else i am missing here.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文