为了开始boost::thread,我写了一个非常简单的例子 - 它不起作用.有谁可以指出我的错误?
我写了一个非常简单的仿函数类来完成这项工作.它应该计算一个std::vector双精度的总和,并给我一个方法来获得后来的结果:
class SumWorker
{
private:
double _sum;
public:
SumWorker() : _sum(-1.0) {}
void operator() (std::vector<double> const & arr)
{
_sum = 0.0;
for(std::vector<double>::const_iterator i = arr.begin();
i != arr.end();
i++)
{
_sum += (*i);
}
}
double const value() const
{
return _sum;
}
};
Run Code Online (Sandbox Code Playgroud)
现在,我可以通过两种方式之一计算总和.如果我在主线程中这样做,比如,
SumWorker S;
S(numbers); // "numbers" is an std::vector<double>
double sum = S.value(); // "sum" now contains the sum
Run Code Online (Sandbox Code Playgroud)
一切正常.但是,如果我尝试在一个单独的线程中执行此操作(这是重点),
SumWorker S;
boost::thread thread(S, numbers); // Should be …Run Code Online (Sandbox Code Playgroud) 我正在努力教自己Prolog.下面,我写了一些代码,我认为它应该返回无向图中节点之间的所有路径......但事实并非如此.我试图理解为什么这个特定的代码不起作用(我认为这个问题与类似的Prolog寻路帖区分开来).我在SWI-Prolog中运行它.有线索吗?
% Define a directed graph (nodes may or may not be "room"s; edges are encoded by "leads_to" predicates).
room(kitchen).
room(living_room).
room(den).
room(stairs).
room(hall).
room(bathroom).
room(bedroom1).
room(bedroom2).
room(bedroom3).
room(studio).
leads_to(kitchen, living_room).
leads_to(living_room, stairs).
leads_to(living_room, den).
leads_to(stairs, hall).
leads_to(hall, bedroom1).
leads_to(hall, bedroom2).
leads_to(hall, bedroom3).
leads_to(hall, studio).
leads_to(living_room, outside). % Note "outside" is the only node that is not a "room"
leads_to(kitchen, outside).
% Define the indirection of the graph. This is what we'll work with.
neighbor(A,B) :- leads_to(A, B). …Run Code Online (Sandbox Code Playgroud)