Kin*_*pin 1 c++ multithreading
for (int i = 0; i < 16; i++)
{
thread myThread(compute_mandelbrot, -2.0, 1.0, 1.125, -1.125, ystart, yend);
ystart = ystart + 30;
yend = yend + 30;
myThread.join();
}
Run Code Online (Sandbox Code Playgroud)
我本质上想要并行运行16个线程,每个线程渲染一个mandelbrot图像.我没有看到我如何实现这是在一个for循环中,因为我必须等待线程在创建新线程之前完成.
有没有办法让我运行并行线程而不必一个接一个地创建16个线程?
好吧,显然你需要在保持跟踪的同时异步运行它们:
// DISCLAIMER: not a real production code
std::vector<std::thread> workers;
// first start them all
for(std::size_t i{}; i < 16; ++i, ystart += 30, yend += 30) {
workers.emplace_back(compute_mandelbrot, -2.0, 1.0, 1.125, -1.125, ystart, yend);
}
// now join them all
for(auto &w: workers) w.join();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
147 次 |
| 最近记录: |