在 Xcode 中编译时,每次尝试等待我的 condition_variable 时,我都会收到错误消息。错误是“以 std::__1::system_error 类型的未捕获异常终止:condition_variable 等待失败:参数无效”
在 Visual Studio 2013 中一切正常。如果我决定不等待来自多个线程的同一个 condition_variable,代码就可以正常工作。呜呜呜。
好的,代码。
主要.cpp:
#include "ThreadPool.h"
int main(int argc, const char * argv[])
{
ThreadPool pool;
for (int i = 0; i < 10; ++i)
pool.sendWork();
std::this_thread::sleep_for(std::chrono::milliseconds(50000));
return 0;
}
线程池.h
#pragma once
#include <condition_variable>
#include <vector>
#include <thread>
class ThreadPool
{
protected:
std::condition_variable _condition;
private:
std::vector<std::thread> _threads;
void threadLoop();
public:
ThreadPool();
void sendWork();
};
线程池.cpp:
#include "ThreadPool.h"
ThreadPool::ThreadPool()
{
for (unsigned int i {0}; i < 10; ++i)
_threads.push_back(std::thread(&ThreadPool::threadLoop, this));
}
void ThreadPool::threadLoop()
{
std::mutex mutex;
std::unique_lock<std::mutex> lock {mutex};
_condition.wait(lock); // This is where the crash happens
}
void ThreadPool::sendWork()
{
_condition.notify_one();
}
这是对实际代码的重大简化,但足以引发崩溃。
这应该有效吗?我在这里遗漏了什么吗?
最佳答案
更准确地说,condition_variable::wait()
规范的 Requires 部分是 (N3936 §30.5.1 [thread.condition.condvar]/p9):
void wait(unique_lock<mutex>& lock);
Requires:
lock.owns_lock()
istrue
andlock.mutex()
is locked by the calling thread, and either— no other thread is waiting on this
condition_variable
object or—
lock.mutex()
returns the same value for each of the lock arguments supplied by all concurrently waiting (viawait
,wait_for
, orwait_until
) threads.
因此,每个并发等待 condition_variable
的线程都必须传递相同的互斥量。技术上允许两个线程使用一个互斥锁等待 condition_variable
,然后在两个等待终止后(没有线程在等待 condition_variable
),让它们两者都使用不同的互斥量等待同一个对象。我不知道这是否有任何现实的用例。
另请注意,std::condition_variable_any::wait()
没有这样的要求。对提供的类型的唯一要求是它满足 BasicLockable
要求,这基本上意味着它具有 lock()
和 unlock()
。
关于c++ - condition_variable 使用 Xcode 抛出 system_error - 使用 VStudio 没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24846995/