正在等待atomic_bool

Waiting for an atomic_bool

本文关键字:bool atomic 在等待      更新时间:2023-10-16

我有两个线程和一个由第二个线程设置的标志。我可以使用atomic_bool,但我希望能够等待*在第一个线程上设置标志。我该怎么做?

我想我不能使用condition_variable,因为如果第二个线程在第一个线程开始等待之前调用notify_one,那么线程就不会唤醒。

此外,检查标志是否已经设置应该相当快。我想这应该很简单,但我只是被卡住了,所以我在这里问。提前谢谢。

*编辑:当然是块,不忙等。如果不清楚的话,我很抱歉。

在cbreak和Ravadre(评论)的帮助下,我从这里得到了:

int main()
{
    std::mutex m;
    std::condition_variable cv;
    std::thread t([&] {
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::unique_lock<std::mutex> lock(m);
            cv.wait(lock);
            std::cout << "Yay!n";
    });
    cv.notify_one();
    t.join();
}

它通常根本不会终止,到这里:

int main()
{
    std::mutex m;
    std::condition_variable cv;
    bool flag = false;
    std::thread t([&] {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::unique_lock<std::mutex> lock(m);
        cv.wait(lock, [&] { return flag; });
        std::cout << "Yay!n";
    });
    {
        std::lock_guard<std::mutex> lock(m);
        flag = true;
    }
    cv.notify_one();
    t.join();
}

它确实完成了任务,但似乎仍然有很多不必要的开销。请随意发布一个等效但更具性能(或更优雅)的答案,我很乐意接受。不过,请只使用标准C++11,如果不使用,请解释为什么标准C++11不能做到这一点。

编辑:我还写了一个类safe_flag来封装它(再次感谢cbreak);请随时提出任何改进建议。

class safe_flag
{
    mutable std::mutex m_;
    mutable std::condition_variable cv_;
    bool flag_;
public:
    safe_flag()
        : flag_(false)
    {}
    bool is_set() const
    {
        std::lock_guard<std::mutex> lock(m_);
        return flag_;
    }
    void set()
    {
        {
            std::lock_guard<std::mutex> lock(m_);
            flag_ = true;
        }
        cv_.notify_all();
    }
    void reset()
    {
        {
            std::lock_guard<std::mutex> lock(m_);
            flag_ = false;
        }
        cv_.notify_all();
    }
    void wait() const
    {
        std::unique_lock<std::mutex> lock(m_);
        cv_.wait(lock, [this] { return flag_; });
    }
    template <typename Rep, typename Period>
    bool wait_for(const std::chrono::duration<Rep, Period>& rel_time) const
    {
        std::unique_lock<std::mutex> lock(m_);
        return cv_.wait_for(lock, rel_time, [this] { return flag_; });
    }
    template <typename Rep, typename Period>
    bool wait_until(const std::chrono::duration<Rep, Period>& rel_time) const
    {
        std::unique_lock<std::mutex> lock(m_);
        return cv_.wait_until(lock, rel_time, [this] { return flag_; });
    }
};
bool go = false;
std::mutex mtx;
std::condition_variable cnd;
// waiting thread:
std::unique_lock<std::mutex> lck(mtx);
while (!go)
    cnd.wait(lock);
// when we get here we know that go is true, and we have the lock
// signalling thread:
{
std::unique_lock<std::mutex> lck(mtx);
go = true;
cnd.notify_one();
}
// now we've released the lock, so the waiting thread will make progress

您的平台究竟是什么?在posix兼容平台上,我们使用

  sem_t semaphore;
  sem_init( &semaphore , 0 , x );

以获得初始值为x的信号量。然后使用

 sem_wait(&semaphore ); sem_post(&semaphore);

您可以同步这两个线程。请记住将semaphore声明为全局变量,以确保两个线程都可以访问它(或通过任何其他实现相同功能的方法)。

长话短说,你可以:

sem_t semaphore;
sem_init(&semaphore, 0 , 0 );
void thread2(){
   sem_post(&semaphore);    //second thread --A
}
void thread1(){
    sem_wait(&semaphore);   // wait until thread2() executes line A
}

应该有类似的实用程序来在Win32上实现相同的功能。