简单的线程池不会嗡嗡作响

Simple thread pool won't buzz

本文关键字:线程 简单      更新时间:2023-10-16

尝试编写一个简单的线程池。只有第一个thread_t被初始化,它有点挂起。我无法继续。需要帮助

class thread_t
{
public:
    thread_t(int id, bool& running)
        :id_(id)
        , running_(running)
    {
        idle_ = true;
        thread_ = new thread([=]() { run(); });
    }
    ~thread_t()
    {
        thread_->join();
    }
private:
    void run()
    {
        cout << id_ << "  starting  n";
        while (running_)
        {
            this_thread::sleep_for(chrono::milliseconds(10ms));
        }
    }
private:
    thread* thread_;
    bool idle_;
    int id_;
    bool& running_;
};
class pool
{
public:
    pool(int n, bool& running)
        :nthreads_(n)
        ,running_(running)
    {
        if (n > std::thread::hardware_concurrency()) nthreads_ = n = std::thread::hardware_concurrency();
        for (int i = 0; i < n; i++)
        {
            threads_.push_back(thread_t(i, running_));
        }
    }
private:
    vector<thread_t> threads_;
    int nthreads_;
    bool& running_;
};

//queue < function<void(void)>> tasks;
int main()
{
    bool running = true;
    pool pool1(5, running);
    this_thread::sleep_for(chrono::seconds(5s));
    running = false;
    return 0;
}

您的代码正在尝试加入您立即创建的第一个线程。 从gdb

#0  0x00007ffff729eb6d in pthread_join () from /lib64/libpthread.so.0
#1  0x00007ffff7ab6223 in __gthread_join (__value_ptr=0x0, 
    __threadid=<optimized out>)
    at /var/tmp/paludis/sys-devel-gcc-7.3.0/work/build/x86_64-pc-linux-gnu/libstdc++-v3/include/x86_64-pc-linux-gnu/bits/gthr-default.h:668
#2  std::thread::join (this=0x55555576bc20)
    at /var/tmp/paludis/sys-devel-gcc-7.3.0/work/gcc-7.3.0/libstdc++-v3/src/c++11/thread.cc:136
#3  0x00005555555553ff in thread_t::~thread_t (this=0x7fffffffd6f0, 
    __in_chrg=<optimized out>) at thread.cpp:21
#4  0x000055555555559b in pool::pool (this=0x7fffffffd740, n=5, 
    running=@0x7fffffffd737: true) at thread.cpp:50
#5  0x0000555555555151 in main () at thread.cpp:66

Stephen Newwell 正确地指出 vector::p ush_back 确实在调用析构函数 (+1 给他(。现在我使用unique_ptr它现在只解决了我的问题。

编辑:刚刚完成这个项目,如果有人感兴趣,这里是

链接
class thread_t
{
public:
    thread_t(int id, bool& running)
        :id_(id)
        , running_(running)
    {
        idle_ = true;
        thread_ = new thread([=]() { run(); });
    }
    ~thread_t()
    {
        cout << id_ << "  killing  n";
        thread_->join();
    }
private:
    void run()
    {
        cout << id_ << "  starting  n";
        while (running_)
        {
            this_thread::sleep_for(chrono::milliseconds(10ms));
        }
    }
private:
    thread* thread_;
    bool idle_;
    int id_;
    bool& running_;
};
class pool
{
public:
    pool(int n, bool& running)
        :nthreads_(n)
        ,running_(running)
    {
        if (n > std::thread::hardware_concurrency()) nthreads_ = n = std::thread::hardware_concurrency();
        for (int i = 0; i < n; i++)
        {
            threads_.push_back(make_unique<thread_t >(i, running_));
        }
    }
private:
    vector<unique_ptr<thread_t>> threads_;
    int nthreads_;
    bool& running_;
};

//queue < function<void(void)>> tasks;
int main()
{
    bool running = true;
    pool pool1(5, running);
    this_thread::sleep_for(chrono::seconds(5s));
    running = false;
    return 0;
}