多线程无法正确连接

时间:2019-03-06 06:13:21

标签: c++ multithreading condition-variable

print(df.set_index(['region_id', 'statistic'])
   .stack()
   .str.split(', ', expand=True)
   .stack()
   .unstack(-2)
   .reset_index(-1, drop=True)
   .reset_index()
)

我只想在多线程中测试条件变量。 上面的代码是我的测试代码。

错误是线程t1无法正确加入。我打印#include <iostream> #include <vector> #include <thread> #include <mutex> #include <condition_variable> #include <atomic> using namespace std; mutex m; condition_variable cov; bool ready = false; bool processed = false; void showNum(int &f_, atomic_bool &alive_) { while(alive_) { unique_lock<mutex> lk(m); cov.wait(lk,[]{ return ready;}); f_++; ready = false; processed= true; lk.unlock(); cout<<f_<<endl; cov.notify_one(); } } int main() { vector<int> va; for (int i = 0; i < 10; ++i) { va.push_back(i); } int f = 0; atomic_bool alive{ true }; std::thread t1(showNum,ref(f),ref(alive)); auto sizeofVector = va.size(); for (int j = 0; j < sizeofVector; ++j) { { lock_guard<mutex> lk0(m); f = va.back(); cout<<f<<" "; ready = true; } cov.notify_one(); va.pop_back(); { unique_lock<mutex> lk(m); cov.wait(lk,[]{return processed;}); processed = false; lk.unlock(); } } alive = false; t1.join(); return 0; } ,它始终是真实的,不能由主线程中的alive_设置为false。

我尝试使alive = false成为全局变量,但仍然是相同的错误。

能给我一些建议吗?

3 个答案:

答案 0 :(得分:2)

可以更改

cov.wait(lk,[]{ return ready;});

cov.wait(lk,[&alive_]{ return ready || !alive_;});
if (!alive_)
    break;

alive_=false;下添加行

cov.notify_one();

完整代码如下

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
using namespace std;
mutex m;
condition_variable cov;
bool ready = false;
bool processed = false;
void showNum(int &f_, atomic_bool &alive_)
{
    while(alive_)
    {
        unique_lock<mutex> lk(m);
        cov.wait(lk,[&alive_]{return ready || !alive_;});
        if (!alive_)
            break;
        f_++;
        ready = false;
        processed= true;
        lk.unlock();
        cout<<f_<<endl;
        cov.notify_one();
    }

}
int main() {
    vector<int> va;
    for (int i = 0; i < 10; ++i) {
        va.push_back(i);
    }
    int f = 0;
    atomic_bool alive{ true };


    std::thread t1(showNum,ref(f),ref(alive));
    auto sizeofVector = va.size();
    for (int j = 0; j < sizeofVector; ++j) {
        {
            lock_guard<mutex> lk0(m);
            f = va.back();
            cout<<f<<"    ";
            ready = true;
        }

        cov.notify_one();
        va.pop_back();
        {
            unique_lock<mutex> lk(m);
            cov.wait(lk,[]{return processed;});
            processed = false;
            lk.unlock();
        }

    }

    alive = false;
    cov.notify_one();

    t1.join();
    return 0;
}

答案 1 :(得分:1)

t1中,该函数不会不断测试alive。您已经对其进行了设计,以便每个循环都从等待条件变量开始。然后,只有在收到通知时,它才会进入睡眠和唤醒状态。不幸的是,当main设置为false时,t1线程仍处于等待状态。

您可以轻松观察到这一点:

void showNum(int &f_, atomic_bool &alive_)
{
    while(alive_)
    {   cout<<"waiting..."<<endl;   
        unique_lock<mutex> lk(m);
        cout<<"waiting more..."<<endl;
        cov.wait(lk,[]{ return ready;});  ///<<<<< stuck here 
        cout<<"go..."<<endl;
        f_++;
        ready = false;
        processed= true;
        lk.unlock();
        cout<<"  sn:"<<f_<<endl;
        cov.notify_one();
    }
}

仅当main将确保在条件变量上再进行一次通知时,它才会唤醒。仅在此刻,它才会退出其等待状态,并且在处理后发现alivefalse

为避免永远卡住,您可以更改代码并使用wait_for(),以便该函数可以检查是否仍应停留在alive上。

答案 2 :(得分:0)

#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <chrono>
using namespace std;
mutex m;
condition_variable cov;
bool ready = false;
bool processed = false;
atomic_bool alive{ true };
void showNum(int &f_, atomic_bool &alive_)
{
    while(alive)
    {
        unique_lock<mutex> lk(m);
        cov.wait(lk,[]{ return ready || !alive;});
        if(!alive)
            break;
        f_++;
        ready = false;
        processed= true;
        lk.unlock();
        cout<<f_<<endl;
        cov.notify_one();
    }

}
int main() {
    vector<int> va;
    for (int i = 0; i < 10; ++i) {
        va.push_back(i);
    }
    int f = 0;



    std::thread t1(showNum,ref(f),ref(alive));
    auto sizeofVector = va.size();
    for (int j = 0; j < sizeofVector; ++j) {
        {
            lock_guard<mutex> lk0(m);
            f = va.back();
            cout<<f<<"    ";
            ready = true;
        }

        cov.notify_one();
        va.pop_back();
        {
            unique_lock<mutex> lk(m);
            cov.wait(lk,[]{return processed;});
            processed = false;
            lk.unlock();
        }

    }
    alive = false;
    cov.notify_one();
    t1.join();
    return 0;
}

集成建议后,我将代码修改为上面的代码。它输出符合我的预期。 感谢所有提供建议和问候的人。