std::线程实例计数不等于实际应用程序线程计数
std::thread instances count isn't equal to actual application threads count
我已经创建了大约20个std::线程,但是我的系统监视器(Mac OS X Maveriscks及其调试器LLDB)只显示给我5个线程。怎么了?为什么如果我尝试创建20个线程,我反而得到5个线程?
try
{
std::promise<bool> prm[ 20 ] = {};
std::thread thr[ 20 ] = {};
for (int i = 0; i < 20; ++i)
{
thr[ i ] = std::thread(
[&prm, i]()
{
try
{
while ( ! findPasswd() );
prm[ i ].set_value( true );
}
catch(...)
{
prm[ i ].set_exception( std::current_exception() );
}
}
);
thr[ i ].detach();
}
for (auto & pr : prm)
{
pr.get_future().get();
}
}
这次与detach
我有足够的线程,但之前我尝试join
代替detach
,只有五个线程。
解决方案不是在线程创建后立即加入线程,而是在线程创建循环结束后加入。
相关文章:
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 通过安装信号处理程序关闭多线程应用程序
- 将数组作为多线程应用程序中函数的返回传递
- 修改多线程应用程序中的对象
- C++多线程应用程序将永远挂起
- 具有多线程应用程序的 Nanomsg 无阻塞双向套接字
- 在接收 SIGINT 操作时适当地关闭多线程 c++ 应用程序
- 如何在多线程应用程序中获得花费的时间
- Linux VM(重型多线程应用程序)的性能改进
- 在C 多线程应用程序中,设置了并发线程的最大数量
- C++多线程应用程序崩溃
- 多线程应用程序中的零MQ处理中断
- 使用C 的多线程应用程序中测量全局时间(壁挂)的最快方法
- 如何在多线程应用程序中使用 pybind11
- 多线程应用程序中的独立日志记录
- 如何在不冻结线程/应用程序的情况下减慢方法执行速度
- 设计一个扩展良好的多线程应用程序
- 在 Linux 上为 Windows 编译多线程应用程序 [ C++ ]
- 如何为多线程应用程序创建全局对象
- 管理多线程应用程序中的共享变量