如何重置std :: condition_variable

时间:2016-02-01 20:29:29

标签: c++ multithreading thread-safety

我正在尝试使用条件变量来触发第二个线程在一定数量的缓冲区处于双端队列中之后执行某些工作。

  • main()正在紧密且时间敏感的循环中读取ADC数据
  • main()将缓冲区添加到双端队列(由线程池处理) 管理器)
  • deque深度是可变的,所以在main()中测试深度> = 4和 然后调用notify_one是不可能的(即:在重处理期间 时间,双端队列的深度可能高达200或300个缓冲区 或在光处理过程中只有4)
  • thrd()永远不需要发信号main()

我的问题是在thrd()中,在获取信号和缓冲区之后< 4,它循环通过while循环并立即再次获得条件。有没有办法重置简历?

deque<int> vbufs;
std::mutex thrdLock;
void thrd()
{
    while (true) {
        cout << "thrd() waiting for lock\n";
        std::unique_lock < std::mutex > lock(thrdLock);
        cout << "thrd() waiting for condition\n";
        cv.wait(lock, []{ return (vbufs.size() > 0); });
        thrdLock.unlock();
        cout << "thrd() condition set\n";

        if (vbufs.size() >= 4) {    // pretend to do something with 4 buffers and remove them
            std::lock_guard < std::mutex > lock(thrdLock);
            vbufs.pop_front();
            vbufs.pop_front();
            vbufs.pop_front();
            vbufs.pop_front();
            cout << "thrd() reducing buffers:" << vbufs.size() << endl;
        }
    }
}

int main(int argc, char* argv[]) {
    std::thread t1(thrd);
    int tctr = 0;

    while (true) {
        usleep(1000);

        {
            cout << "main() waiting for lock\n";
            std::lock_guard < std::mutex > lock(thrdLock);
            vbufs.push_back(++tctr);
            cout << "main() incremented buffers:" << vbufs.size() << endl;
        }
        cv.notify_one();
    }
    return 0;
}

2 个答案:

答案 0 :(得分:5)

您无法重置条件变量;这没有任何意义 - 通知它不会改变它的状态,所以没有什么可以重置。当通知它时,只有已经等待的线程才能被唤醒。如果该线程再次等待,则在重新通知条件变量之前它不会继续。

如果你只想在有四个或更多缓冲区的情况下工作,你不应该改变你的等待吗?

cv.wait(lock, []{ return (vbufs.size() >= 4); });

更重要的是,您可能同时从两个不同的线程读取和写入vbufs - 您的if (vbufs.size() >= 4)发生在锁定之外,因此可能与调用{同时发生{1}}。这会导致未定义的行为,这可能会解释您所看到的内容。

答案 1 :(得分:0)

我在程序中发现了一些错误 - 在此更正:

注意:直接解锁互斥锁而不是std::unique_lock解锁互斥锁的方式导致数据竞争。

#include <iostream>
#include <deque>
#include <mutex>
#include <thread>
#include <chrono>
#include <condition_variable>

std::deque<int> vbufs;
std::mutex thrdLock;
std::condition_variable cv;

template<class...Ts>
void emit(Ts&&...ts)
{
    static std::mutex m;
    std::lock_guard<std::mutex> lg(m);
    using expand = int[];
    void(expand { 0, ((std::cout << std::forward<Ts>(ts)), 0)... });
}

void thrd()
{
    while (true) {
        emit("thrd() waiting for lock\n");
        std::unique_lock < std::mutex > lock(thrdLock);
        emit("thrd() waiting for condition\n");
        cv.wait(lock, []{ return vbufs.size() >= 4; });
        emit("thrd() condition set\n");
        auto a = vbufs.front(); vbufs.pop_front();
        auto b = vbufs.front(); vbufs.pop_front();
        auto c = vbufs.front(); vbufs.pop_front();
        auto d = vbufs.front(); vbufs.pop_front();
        emit("thrd() reducing buffers:", vbufs.size(), '\n');
        lock.unlock();
        emit(a, ' ', b, ' ', c, ' ', d, '\n');
    }
}

int main(int argc, char* argv[]) {
    std::thread t1(thrd);
    int tctr = 0;

    while (true) {
        std::this_thread::sleep_for(std::chrono::microseconds(10));

        {
            emit("main() waiting for lock\n");
            std::lock_guard < std::mutex > lock(thrdLock);
            vbufs.push_back(++tctr);
            emit("main() incremented buffers:", vbufs.size(), '\n');
        }
        cv.notify_one();
    }
    t1.join(); // un-necessary in this case, but...
    return 0;
}
相关问题