【问题标题】:C++ Future Wrong Value in Release Version发布版本中的 C++ 未来错误值
【发布时间】:2019-09-27 09:14:49
【问题描述】:

我编写了一个小型线程池类,并根据 pschill (https://codereview.stackexchange.com/questions/229613/c-standard-thread-threadpool) 给出的建议对其进行了调整。 我现在已经将几个任务排队,直到一个线程可以处理它。 该任务由enum class State、std::mutex(用于状态)和std::packaged_task<R(Args...)> 组成。任务由调用实例创建(参见代码示例),并确保和测试生命周期。一旦线程准备好,任务就会被处理。 到目前为止,在每个构建配置中一切正常。

Debug x64 构建(最新的 VCpp)的执行给了我4 的预期值。使用任何其他配置都会导致“随机”值....

我检查了每个相关对象的生命周期;正如预期的那样。

任务执行

{
    // May unnecessary but added due to previous errors
    std::lock_guard<std::mutex> 
    m_state = State::INPROGESS;
}

m_task(args...);

{
    std::lock_guard<std::mutex> lock(m_statelock);
    m_state = State::DONE;
}

排队任务

template <typename R, typename ... Args>
void addTask(Task<R, Args...>& task, Args ... args) {
    // m_tasks is a std::deque<std::function<void()>>
    std::lock_guard<std::mutex> lock(m_tasklock);
    m_tasks.emplace_back([&]() -> void {
        task.execute(args...);
    });

    // Has own mutex
    m_task_avaiable.notify_one(); // std::condition_variable
}

线程对象例程

[&]() -> void {
    while (true) {
        while (tpool.m_tasks.size() > 0) {
            {
                // Added due to crash with Release-config at asgining
                // atomic<State> in Task
                std::lock_guard<std::mutex> lock(statelock);
                state = State::INPROGRESS;
            }

            std::function<void()> job;

            {
                std::lock_guard<std::mutex> lock(tpool.m_tasklock);
                if (tpool.m_tasks.size() > 0) {
                    job = std::move(tpool.m_tasks[0]);
                    tpool.m_tasks.pop_front();
                }
            }

            if (job) { job(); }
    }

        {
            std::lock_guard<std::mutex> lock(statelock);
            state = State::IDLE;
        }
        tpool.m_task_avaiable.wait(lock);
        {
            std::lock_guard<std::mutex> lock(statelock);
            if (state == State::STOP) {
                return; // To stop thread and make it joinable
            }

        }
    }
}

主要

frm::util::ThreadPool pool(2);

frm::util::Task<int, int, int> t1([](int a, int b) {
    std::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "Yey!...\n";
    return a + b;
});


pool.addTask(t1, 2, 2);

// Direct access to member std::packaged_task<...> just for debugging
std::cout << t1.m_task.get_future().get() << '\n';

如果有帮助,任务对象 (x32) 的转储显示请求了未来(字节 61):

 class std::mutex   48 Byte
    1   0x02 0x00 0x00 0x00
    5   0xbc 0x78 0x9f 0x0f
    9   0x00 0x00 0x00 0x00
   13   0x00 0x00 0x00 0x00
   17   0x00 0x00 0x00 0x00
   21   0x00 0x00 0x00 0x00
   25   0x00 0x00 0x00 0x00
   29   0x00 0x00 0x00 0x00
   33   0x00 0x00 0x00 0x00
   37   0x00 0x00 0x00 0x00
   41   0xff 0xff 0xff 0xff
   45   0x00 0x00 0x00 0x00
 struct std::atomic<enum frm::util::Task<int,int,int>::State>    4 Byte
   49   0x02 0x00 0x00 0x00
 class std::packaged_task<int __cdecl(int,int)>   12 Byte
   53   0x50 0xbd 0x15 0x01
   57   0x00 0x00 0x00 0x00
   61   0x01 0x00 0x00 0x00

这与 VCpp19 中的 std::future&lt;T&gt;、std::shared_future&lt;T&gt; 或 std::packaged_task&lt;T&gt; 有什么问题还是我错过了什么?

我可以展示更多代码,但由于 SO 的规则和建议,我尽可能少地复制。

可能也有帮助:

在使用 addTask(Task&lt;R, Args...&gt;&amp; task, Args ... args) addTask(Task&lt;R, Args...&gt;* task, Args ... args) 之前,如果添加了如下延迟,则会导致错误(不在 x64 调试中):

// Direct access to member std::packaged_task<...> just for debugging
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << t1.m_task.get_future().get() << '\n';

【问题讨论】:

  • 见this question,它展示了如何将参数包移动到lambda中。

标签: c++ threadpool future


【解决方案1】:

您在以下代码中有未定义的行为:

 m_tasks.emplace_back([&]() -> void {
      task.execute(args...);
   });

您的 lambda 通过 REFERENCE 捕获 args。 emplace_back 将 functor 放入 m_tasks,然后封闭范围结束,当调用任务时,您将访问悬空引用。

你可以按值传递:

 m_tasks.emplace_back([=]() -> void {
    task.execute(args...);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多