小编use*_*797的帖子

boost :: thread - 简单示例不起作用(C++)

为了开始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)

c++ boost-thread

9
推荐指数
2
解决办法
484
查看次数

Prolog中的寻路

我正在努力教自己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)

path prolog path-finding

4
推荐指数
1
解决办法
1981
查看次数

标签 统计

boost-thread ×1

c++ ×1

path ×1

path-finding ×1

prolog ×1