c++ - condition_variable 使用 Xcode 抛出 system_error - 使用 VStudio 没问题

标签 c++ multithreading c++11

在 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() is true and lock.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 (via wait, wait_for, or wait_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/

相关文章:

c++ - 编译 MongoDB C++11 驱动程序时出错

C++20,如何用 Clang-10 或 GCC11 编译

c++ - 如何将 double 或 float(所有数字)转换为 int?

java - 使用 Jest 的服务在线程池上阻塞,为什么?

android - 从 OpenGL 线程使用时 Handler.dispatchMessage 挂起/崩溃

c++ - 只有 "void"是不可引用类型吗?

c++ - 为什么 C++ 编译器不从最终类优化这个 dynamic_cast?

C++ 如何首先从基类调用相同的方法,然后从派生类调用?

c# - 每个托管线程是否都有自己对应的 native 线程?

c++ - 是否将指向第一个成员的指针解释为类本身定义良好?