c++ - std::thread 导致应用程序中止并出现错误 R6010

标签 c++ multithreading c++11 stdthread

我有一个名为 Task 的类,它内部包含一个成员 std::thread。一般的想法是创建一个在处理请求到来时保持事件状态的线程。

class Task
{
public:
    Task();
    ~Task();

    void start();
    // some funny stuff here
protected:
    Task(const Task& ref);
    void main_function();

    std::thread m_thread;
    // more funny stuff like queues, mutexes, etc
}

在函数 start() 中我这样做:

void Task::start()
{
    m_thread = std::thread(std::bind(&Task::main_function, this));
}

问题在于此行调用 abort() 时出现运行时错误 R6010。我在某处读到这可能是由于 m_thread 的析构函数在没有先前加入的情况下被调用引起的,但是由于线程尚未启动,我无法加入它。

我正在使用 Visual Studio 2012 运行它

更新:

所以我尝试了一个测试示例,无法复制错误。然后我按照建议在有问题的函数中替换了我的启动函数:

void Task::start()
{
    assert(!m_thread.joinable());
    m_thread = std::thread(&Task::main_function,this);
}

但我仍然收到错误 R6010。 调用堆栈是:

msvcr110d.dll!_NMSG_WRITE(int rterrnum) Line 226    C
msvcr110d.dll!abort() Line 62   C
msvcr110d.dll!terminate() Line 97   C++
msvcp110d.dll!_Call_func(void * _Data) Line 63  C++

msvcr110d.dll!_callthreadstartex() Line 354 C
msvcr110d.dll!_threadstartex(void * ptd) Line 337   C

更新 2: 终于可以重现问题了。代码如下。在主函数中调用 foo()。

class Task 
{
public:
    Task() : m_exitFlag(false) 
    { 
        std::cout << "constructor called" << std::endl;
    }

    ~Task()
    {
        m_lock.lock();
        m_exitFlag = true;
        m_condlock.notify_all();
        m_lock.unlock();

        if (m_thread.joinable()) m_thread.join();
        std::cout << "destructor called" << std::endl;
     }

     void start()
     {
         std::cout << "Task start" << std::endl;
         assert(!m_thread.joinable());
         m_thread = std::thread(&Task::main_function, this);
     }
protected:
     void main_function()
     {
         std::cout << "thread started" << std::endl;
         while(1)
         {
             m_lock.lock();
             while(m_queue.empty() && !m_exitFlag)
                 m_condlock.wait(std::unique_lock<std::mutex>(m_lock));

             if (m_exitFlag)
             {
                 m_lock.unlock();
                 std::cout << "thread exiting" << std::endl;
                 return;
             }

             std::function<void()> f;
             if (!m_queue.empty()) f = m_queue.front();

             m_lock.unlock;
             if (f != nullptr) f();
         }
     }
     Task(const Task&ref) { }

     Task& operator=(const Task& ref) {
         return *this;
     }
};
void foo() {
    Task tk;
    tk.start();
}

我猜这里某处存在竞争条件,因为它有时会崩溃,而其他时候则不会。 一个线程在 ~Task() 中的临界区内,另一个在 Update1 中作为堆栈。

最佳答案

切勿直接锁定互斥量。 C++ 提供了 lock_guardunique_lock 等。这是有原因的。

特别是,这部分有问题:

m_lock.lock();
while(m_queue.empty() && !m_exitFlag)
    m_condlock.wait(std::unique_lock<std::mutex>(m_lock));

新构造的 unique_lock 将尝试锁定已经锁定的互斥锁 m_lock。这will cause undefined behavior如果互斥锁是 std::mutex,或者如果互斥锁是 std::recursive_mutex,则可能是死锁。 另请注意,当您绑定(bind)未命名的 unique_lock to a non-const reference when calling wait 时,此行依赖于非标准编译器扩展。 .

因此,您要做的第一件事就是让锁成为一个命名变量。然后要么将 std::adopt_lock 传递给锁的构造函数,要么更好,但永远不要直接锁定互斥锁,而是始终将其包装在适当的锁管理类中。

例如,

m_lock.lock();
m_exitFlag = true;
m_condlock.notify_all();
m_lock.unlock();

成为

{
    std::lock_guard<std::mutex> lk(m_lock);
    m_exitFlag = true;
    m_condlock.notify_all();
} // mutex is unlocked automatically as the lock_guard goes out of scope

这还有一个额外的好处,即如果在临界区内抛出异常,您将不会泄漏锁。

关于c++ - std::thread 导致应用程序中止并出现错误 R6010,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415495/

相关文章:

STL - 交换的 noexcept 规范对性能有影响吗?

multithreading - 无锁有界 MPMC 环形缓冲区故障

c++ - 当 -std=c++11 选项被删除时,clang++ 仅使用 boost::format 编译 C++11 程序

c++ - c++ vector中对象的指针问题

c++ - 同步线程 - InterlockedExchange

带有 JSON 处理程序的 C++ 客户端网络库

linux - 与 C++ 初始值设定项有关的内存一致性

multithreading - Perl 线程 - 捕获退出

c++ - 如何获得 std::regex 的所有可能匹配项

c++ - 圆线段碰撞 C++ (UE4)