简单的线程池不会嗡嗡作响
Simple thread pool won't buzz
尝试编写一个简单的线程池。只有第一个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;
}
相关文章:
- 简单使用 std::atomic 在两个线程之间共享数据
- 多线程 gtkmm 应用程序最简单的示例
- 对于 ~95% 写入/5% 读取线程安全的无序列图,有没有一个简单的解决方案?
- 一个简单的 win32 多线程代码.这能行吗?
- 简单的C++循环无法从多线程中受益
- 需要帮助识别简单多线程代码中的错误
- 从线程输出简单向量
- 简单的非阻塞多线程 tcp 服务器
- 简单的线程池不会嗡嗡作响
- 为什么我不能编译这个简单的线程测试?
- 简单线程程序的链接器错误(缺少boost_chrono符号)
- Valgrind 检查简单多线程 C++11 得到 SIG11 错误
- "永远"阻止线程的最简单便携式方法是什么?
- 有没有一种简单的方法可以使用SFML在后台线程中播放声音
- 是否存在简单的指向布尔值作为线程取消标志的现实情况,无法有效地取消线程?
- 使用简单的过程进行慢速多线程处理
- 为什么对于简单的数组复制来说,没有多线程加速
- 为什么std::async与简单的分离线程相比速度较慢
- 使用 Qt 的简单线程 gui 应用程序的结构是什么?
- C++ 11 线程简单示例