尝试编写一个简单的线程池。只初始化了第一个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;
}
答案 0 :(得分:1)
您的代码正在尝试立即加入您创建的第一个帖子。来自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
答案 1 :(得分:0)
Stephen Newwell正确地指出vector :: push_back确实在调用析构函数(+1给他)。现在我使用unique_ptr,它现在只解决了我的问题。
已编辑:如果有人感兴趣link
,请完成此项目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;
}