使用for循环执行线程
Threading with a for loop
我想创建一个从0到4的for循环,但我的问题是我只知道如何每次像这样启动一个线程。
thread t1(eat,"hello");
thread t2(eat,"hello");
所以我的问题是如何在一个循环中一次启动多个线程?
您可以执行以下操作来在vector1中构建它们:
std::vector<std::thread> ts;
for (int i = 5; i > 0; --i)
ts.emplace_back(eat, "hello");
1提前预留内存(假设您知道多少内存)也可以是好的
相关文章:
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- C++线程:如何在一个线程仍在运行时阻止另一个线程执行 (Win32)
- 在给定时间段内,线程执行的指令数量是否有最小数量?
- 如何在C++中停止线程执行
- 为什么Win API线程执行函数,而标准线程则没有
- 线程执行在IDE中和C 中的EXE应用程序中是否有所不同
- 从线程执行的函数中返回结构数组
- 为什么在从线程执行方法时使用QMetaObject::invokeMethod
- 只有一个线程执行 CUDA 内核
- C++ 多线程:执行顺序
- async_connect()超时,多个线程执行io_service.run()
- 是否可以将线程执行转移到另一个线程?
- 具有随机数总和的多线程执行时间
- 如何 在没有 Boost 的情况下将 C++ 成员函数作为线程执行
- openMP 在自定义容器中从 2 到 4 个线程执行二进制搜索时速度变慢
- 如何使生成的线程拖延足够长的时间以使生成线程执行某些操作
- 远程线程执行
- C++中的基本多线程(执行顺序)
- 线程池中的C++线程执行顺序
- 锁定由不同线程执行的多个函数