在struct中声明一个原子变量来执行原子获取并在其上添加指令

时间:2015-04-04 20:55:17

标签: c++ multithreading atomic

I am trying to evaluate the working of asynchronous push relabel algorithm to find maximum flow in a network using C++ and pthread library. The atomic read-modify-write operation should be implemented by atomic fetch-and-add instructions. I run this code in Eclipse Juno for parallel implementation. 

我正在使用2个结构Edge和Push Relabel。我不知道如何将struct中的变量初始化为原子并在线程启动例程内对变量执行原子函数。

工作序列码:(“cap”变量为非原子整数变量)

struct Edge  {
  int from, to, cap, flow, index;
  Edge(int from, int to, int cap, int flow, int index) :
     from(from), to(to), cap(cap), flow(flow), index(index) {}

};

struct PushRelabel {
  int N;
  vector<vector<Edge> > G;
  vector<LL> excess;
  vector<int> dist, active, count;
  queue<int> Q;

  PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}

  void AddEdge(int from, int to, int cap) {
    G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
    if (from == to) G[from].back().index++;
    G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  }

在上面的串行实现中,为了使它以多线程方式工作,我将对结构Edge atomic的“cap”变量使用原子获取和添加操作。所以我需要将Edge结构中的“cap”变量声明为原子。请告诉我怎么做。请在下面找到我在尝试使“cap”变量为原子时对代码所做的更改。

修改后的代码(错误)(“cap”变量为原子整数变量)

    #include <atomic>

    struct Edge  {
 // std::atomic<int> cap = ATOMIC_VAR_INIT(10);
  int from, to,flow, index;
  std::atomic<int> cap;
  Edge(int from, int to, std::atomic<int> cap , int flow, int index) :
     from(from), to(to), cap(cap), flow(flow), index(index) {}
};

    struct PushRelabel {
  int N;
  vector<vector<Edge> > G;
  vector<LL> excess;
  vector<int> dist, active, count;
  queue<int> Q;
  vector<int> returnexcess;

  PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N), returnexcess(N) {}

  void AddEdge(int from, int to, int cap) {
    G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
    if (from == to) G[from].back().index++;
    G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  }
p is the object created for PushRelabel structure. 

以下是需要原子化的操作。

p.G[smallestHtVertex][p.G[privQ.front()][smallVindex].index].cap += amt;

在eclipse中收到以下错误:      - 使用已删除的函数'std :: atomic :: atomic(const std :: atomic&amp;)'      - 隐式删除'Edge :: Edge(Edge&amp;&amp;)'因为默认定义不合适      形成的:

我已经在GCC C ++编译器中包含了-std = c ++ 0x标志。请指导我这个。

0 个答案:

没有答案