【问题标题】:Thread pool not completing all tasks线程池未完成所有任务
【发布时间】:2022-01-19 23:05:23
【问题描述】:

我之前问过这个问题的简单版本,得到了正确答案:Thread pools not working with large number of tasks 现在我正在尝试使用线程池从一个类的对象并行运行任务。我的任务很简单,只为该类实例打印一个数字。我期待数字 0->9 被打印,但我得到一些数字被打印不止一次,而一些数字根本没有打印。谁能看到我在循环中创建任务时做错了什么?

#include "iostream"
#include "ThreadPool.h"
#include <chrono>
#include <thread>

using namespace std;
using namespace dynamicThreadPool;

class test {
    int x;
public:
    test(int x_in) : x(x_in) {}
    void task()
    {
        cout << x << endl;
    }
};

int main(void)
{
    thread_pool pool;
    for (int i = 0; i < 10; i++)
    {
        test* myTest = new test(i);
        std::function<void()> myFunction = [&] {myTest->task(); };
        pool.submit(myFunction);
    }
    while (!pool.isQueueEmpty())
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        cout << "waiting for tasks to complete" << endl;
    }

    return 0;
}

这是我的线程池,我从“C++ Concurrency in Action”一书中得到了这个定义:

#pragma once
#include <queue>
#include <future>
#include <list>
#include <functional>
#include <memory>

template<typename T>
class threadsafe_queue
{
private:
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;
public:
    threadsafe_queue() {}
    void push(T new_value)
    {
        std::lock_guard<std::mutex> lk(mut);
        data_queue.push(std::move(new_value));
        data_cond.notify_one();
    }
    void wait_and_pop(T& value)
    {
        std::unique_lock<std::mutex> lk(mut);
        data_cond.wait(lk, [this] {return !data_queue.empty(); });
        value = std::move(data_queue.front());
        data_queue.pop();
    }
    bool try_pop(T& value)
    {
        std::lock_guard<std::mutex> lk(mut);
        if (data_queue.empty())
            return false;
        value = std::move(data_queue.front());
        data_queue.pop();
        return true;
    }
    bool empty() const
    {
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.empty();
    }
};

class join_threads
{
    std::vector<std::thread>& threads;
public:
    explicit join_threads(std::vector<std::thread>& threads_) : threads(threads_) {}
    ~join_threads()
    {
        for (unsigned long i = 0; i < threads.size(); i++)
        {
            if (threads[i].joinable())
            {
                threads[i].join();
            }
        }
    }
};

class thread_pool
{
    std::atomic_bool done;
    threadsafe_queue<std::function<void()> > work_queue;
    std::vector<std::thread> threads;
    join_threads joiner;
    void worker_thread()
    {
        while (!done)
        {
            std::function<void()> task;
            if (work_queue.try_pop(task))
            {
                task();
            }
            else
            {
                std::this_thread::yield();
            }
        }
    }
public:
    thread_pool() : done(false), joiner(threads)
    {
        unsigned const thread_count = std::thread::hardware_concurrency();
        try
        {
            for (unsigned i = 0; i < thread_count; i++)
            {
                threads.push_back(std::thread(&thread_pool::worker_thread, this));
            }
        }
        catch (...)
        {
            done = true;
            throw;
        }
    }
    ~thread_pool()
    {
        done = true;
    }
    template<typename FunctionType>
    void submit(FunctionType f)
    {
        work_queue.push(std::function<void()>(f));
    }
    bool isQueueEmpty()
    {
        return work_queue.empty();
    }
};

【问题讨论】:

  • 这个问题和stackoverflow.com/questions/70355938/…有什么区别?
  • 我的问题是我没有等待所有线程完成。我在这里等着,所有线程都完成了,但我在这里得到了不正确的结果,因为我希望每个数字只打印一次,但我的输出中有重复,有些数字根本不打印。

标签: c++ multithreading pointers native


【解决方案1】:

代码太多,无法全部分析,但您在此处通过引用获取指针:

{
    test* myTest = new test(i);
    std::function<void()> myFunction = [&] {myTest->task(); };
    pool.submit(myFunction);
} // pointer goes out of scope

在该指针超出范围后,如果您稍后执行myTest-&gt;task();,您将有未定义的行为

为了解决这个直接的问题,复制指针和delete 之后对象不会泄漏内存:

{
    test* myTest = new test(i);
    std::function<void()> myFunction = [=] {myTest->task(); delete myTest; };
    pool.submit(myFunction);
}

我怀疑这完全可以不使用new 来解决,但我会留给你。

【讨论】:

    猜你喜欢
    • 2013-08-14
    • 2019-10-24
    • 2012-08-11
    • 2020-03-27
    • 2011-06-02
    • 1970-01-01
    • 2011-01-30
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多