线程池未完成所有任务

Thread pool not completing all tasks

我之前问过这个问题的简单版本并得到了正确答案: 现在,我正在尝试使用线程池从 class 的对象并行执行 运行 任务。我的任务很简单,只为 class 的实例打印一个数字。我期待数字 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++ 并发实战》一书中得到了这个定义:

#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();
    }
};

代码太多,无法全部分析,但您在这里引用了一个指针:

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

在该指针超出范围后,如果您稍后执行 myTest->task();.

,您将有 未定义的行为

要解决眼前的问题,请复制指针和 delete 之后的对象,以免内存泄漏:

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

我怀疑根本不用 new 就可以解决这个问题,但我会把它留给你。