boost::threadpool
我正在尝试使用(不是boost
, link的官方部分)来并行化我的程序的某个方面。但是,我发现我的程序停顿了,经过检查,htop
显示有两个非活动线程(我怀疑在我的线程池中)和一个线程以 100% 运行(我怀疑我的主执行线程)。
以下是相关代码:
namespace rt {
class Renderer
{
public:
Renderer(int threads) : tp(threads) {}
void render(const Scene&, const Camera&, Image&) const;
private:
mutable boost::threadpool::pool tp;
//int tp;
static void render(const Scene&, const Camera&, Image&, int, int);
static Color trace_ray(const Ray&, const Scene&, int depth = 0);
};
} // namespace rt
void
Renderer::render(const Scene& scene, const Camera& cam, Image& image) const
{
for (int y = 0; y < image.get_height(); ++y)
for (int x = 0; x < image.get_width(); ++x)
tp.schedule(boost::bind(&Renderer::render, scene, cam, image, x, y));
tp.wait();
}
void
Renderer::render(const Scene& scene, const Camera& cam, Image& image, int x, int y)
{
Color c = trace_ray(cam.spawn_ray(x + .25f, y + .25f), scene)
+ trace_ray(cam.spawn_ray(x + .75f, y + .25f), scene)
+ trace_ray(cam.spawn_ray(x + .25f, y + .75f), scene)
+ trace_ray(cam.spawn_ray(x + .75f, y + .75f), scene);
image.set_pixel(x, y, c / 4.0f);
}
我怀疑问题出在我的boost::bind
构造上的原因是,当我创建一个void foobar() {}
函数并将其传递给boost::threadpool::pool::schedule
时,程序不会进入它的无限循环。我在这里做错了什么?