- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当 condition_variable_any
与 recursive_mutex
一起使用时,recursive_mutex
是否通常可从其他线程获取,同时 condition_variable_any::wait
正在等待?我对 Boost 和 C++11 实现都很感兴趣。
这是我主要关心的用例:
void bar();
boost::recursive_mutex mutex;
boost::condition_variable_any condvar;
void foo()
{
boost::lock_guard<boost::recursive_mutex> lock(mutex);
// Ownership level is now one
bar();
}
void bar()
{
boost::unique_lock<boost::recursive_mutex> lock(mutex);
// Ownership level is now two
condvar.wait(lock);
// Does this fully release the recursive mutex,
// so that other threads may acquire it while we're waiting?
// Will the recursive_mutex ownership level
// be restored to two after waiting?
}
最佳答案
通过对 Boost 文档的严格解释,我得出结论 condition_variable_any::wait
通常不会导致 recursive_mutex
在等待通知时被其他线程获取。
Class
condition_variable_any
template<typename lock_type> void wait(lock_type& lock)
Effects:
Atomically call
lock.unlock()
and blocks the current thread. The thread will unblock when notified by a call tothis->notify_one()
orthis->notify_all()
, or spuriously. When the thread is unblocked (for whatever reason), the lock is reacquired by invokinglock.lock()
before the call to wait returns. The lock is also reacquired by invokinglock.lock()
if the function exits with an exception.
所以 condvar.wait(lock)
会调用lock.unlock
,它又调用 mutex.unlock
,这会将所有权级别降低一级(并且不一定降低到零)。
我已经编写了一个测试程序来证实我的上述结论(对于 Boost 和 C++11):
#include <iostream>
#define USE_BOOST 1
#if USE_BOOST
#include <boost/chrono.hpp>
#include <boost/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/recursive_mutex.hpp>
namespace lib = boost;
#else
#include <chrono>
#include <thread>
#include <condition_variable>
#include <mutex>
namespace lib = std;
#endif
void bar();
lib::recursive_mutex mutex;
lib::condition_variable_any condvar;
int value = 0;
void foo()
{
std::cout << "foo()\n";
lib::lock_guard<lib::recursive_mutex> lock(mutex);
// Ownership level is now one
bar();
}
void bar()
{
std::cout << "bar()\n";
lib::unique_lock<lib::recursive_mutex> lock(mutex);
// Ownership level is now two
condvar.wait(lock); // Does this fully release the recursive mutex?
std::cout << "value = " << value << "\n";
}
void notifier()
{
std::cout << "notifier()\n";
lib::this_thread::sleep_for(lib::chrono::seconds(3));
std::cout << "after sleep\n";
// --- Program deadlocks here ---
lib::lock_guard<lib::recursive_mutex> lock(mutex);
value = 42;
std::cout << "before notify_one\n";
condvar.notify_one();
}
int main()
{
lib::thread t1(&foo); // This results in deadlock
// lib::thread t1(&bar); // This doesn't result in deadlock
lib::thread t2(¬ifier);
t1.join();
t2.join();
}
我希望这可以帮助其他人在混合时面临同样的困境 condition_variable_any
和 recursive_mutex
.
关于c++ - condition_variable_any 与 recursive_mutex 一起使用时的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11752155/
使用 POSIX 条件变量,你可以这样写: while (!_doneWaiting) { wait(lock); } 起初当我看到新的 C++11 风格时,我很兴奋: unique_lock
我正在寻找的是类似于 linux 内核中的 wait_queue_t 的东西。我与同步相关的底层数据结构是无锁的,因此不需要互斥锁的保护。 必须获取 std::mutex 才能使用 std::cond
我有一部分代码,其中一个线程调用如下内容: cond->notify_all(); delete cond; 与 std::condition_variable_any cond; Afaik,这应该
当 condition_variable_any 与 recursive_mutex 一起使用时,recursive_mutex 是否通常可从其他线程获取,同时 condition_variable_
假设有三个线程A、B、C,B和C在某个点挂起,等待A发信号让它们继续。在标准 C++ 提供的线程同步工具中,std::condition_variable 似乎最适合放在这里(尽管仍然不好)。由于 s
我在很多地方都读到过一些与 std::condition_variable_any 相关的开销。只是想知道,这是什么开销? 我的猜测是,由于这是一个通用的条件变量,可以与任何类型的锁一起使用,它需要手
最近,我在 Microsoft Visual Studio 2012(配备 Visual C++ 11)上遇到了奇怪的编译问题。 我移植了大型代码库,主要是在 Linux 上开发的,同时考虑到 C++
条件变量应该有关于 notify() 的单一顺序和 unlock_sleep() (在 wait() 中使用的一个虚构的函数调用,其中互斥锁被解锁并且线程作为一个原子操作序列休眠)操作。使用任意可锁定
std::conditional_variable_any 的实现需要(在 gcc 和 clang 中)一个 std::shared_ptr。 在 wait 方法中,互斥锁的生命周期将延长到本地范围。
是否需要同步std::condition_variable/condition_variable_any::notify_one? 据我所知,如果丢失通知是可以接受的 - 可以调用未 protecte
我可能遗漏了一些明显的东西,但我看不出 std::condition_variable 和 std::condition_variable_any 之间有任何区别。为什么我们都需要? 最佳答案 区别在
我是一名优秀的程序员,十分优秀!