正在等待atomic_bool
Waiting for an atomic_bool
我有两个线程和一个由第二个线程设置的标志。我可以使用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上实现相同的功能。
相关文章:
- 写入向量<向量<bool>>
- 如何从 std::atomic 中提取指针 T<T>?
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- std::memory_order for std::atomic:<T>:wait
- MESI协议和std::atomic-它是否确保所有写入立即对其他线程可见?
- 让bool方法返回其他整数
- 如何在 std::vector 中找到<bool>哪些索引是真的?
- Arduino-C++ bool 不会从 false 变为 true
- 为什么在这种情况下,bool 类型的输出等于 0?
- 显式 std::exception_ptr 转换为 bool 不存在.VS2010 错误?
- 错误:不能使用"显式"说明符声明 bool'
- 为什么 C++ 11 在 ios 类中添加了运算符 bool
- 将 std::atomic<bool> 与 std::mutex 结合使用的正确性
- 在 c++ 中使用 std::atomic 的比较和读/写操作<bool>?
- 引用 std::atomic <bool>的已删除函数错误
- std::<bool>atomic和lambda
- 为什么 std::atomic<bool> 比挥发性布尔值慢得多?
- 赋值是否等同于 std::atomic <bool>的加载/存储
- 使用 std::atomic::atomic 时出现错误 C2248<bool>
- C++ std::atomic:<bool>:fetch_or 未实现?