将 boost::threadpool 与 boost::bind 一起使用会使我的程序陷入无限循环
我正在尝试使用 boost::threadpool
(不是 boost
的官方部分,链接)。但是,我发现我的程序停止运行,经过检查,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,程序不会进入无限循环。我在这里做错了什么?
I'm trying to parallelise a certain aspect of my program using boost::threadpool
(not an official part of boost
, link). However, I am finding my program stalls and upon inspection, htop
shows me that there are two inactive threads (I suspect in my threadpool) and one thread running at 100% (I suspect my main execution thread).
Here is the relevant code:
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);
}
The reason I suspect the problem lies with my boost::bind
construct is that when I create a void foobar() {}
function and pass that to boost::threadpool::pool::schedule
, the program does not get into it's infinite loop. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给
boost::bind
的参数 将被复制。在您的情况下:
将
cam
、image
、x
和y
的副本发送到Rendered: :渲染
。这就是你的意图吗?The arguments given to
boost::bind
will be copied.In your case:
will send copies of
cam
,image
,x
, andy
toRendered::render
. Is that what you intend?您是否考虑过使用 boost::thread_group 作为临时线程池?这里使用的 wait() 的实现是什么,这个名字意味着一个障碍,这可能就是你的线程无限期地变得不活动的原因。
编辑:
您可以在不进入无限循环的情况下调用渲染吗?也许是你追踪或产生光线的方式没有显示出来。此外,根据您发布到其实现的链接的简要介绍,您似乎可能希望在线程池上调用
wait_for_all_tasks
。Have you considered using boost::thread_group as impromptu thread pool instead? What is the implementation of wait() being used here the name would imply a barrier which could be why your threads are becoming inactive indefinitely.
Edit:
Can you call render without going into an infinite loop? Perhaps it is in the way you are tracing or spawning rays which is not shown. Also it looks like you may want to call
wait_for_all_tasks
on your thread pool based on a brief glimpse at the link you posted to it's implementation.