c++11g++stdvectorcilk-plus

G++ -cilkplus random behavior with std::vectors


The following (reduced) code is very badly handled by the series of GCC

#include <vector>
#include <cilk/cilk.h>

void walk(std::vector<int> v, int bnd, unsigned size) {
  if (v.size() < size)
    for (int i=0; i<bnd; i++) {
      std::vector<int> vnew(v);
      vnew.push_back(i);
      cilk_spawn walk(vnew, bnd, size);
    }
}

int main(int argc, char **argv) {
  std::vector<int> v{};
  walk(v , 5, 5);
}

Specifically:

My question : is there something wrong with that code. Am I invoking some undefined behavior or is it simply a bug I should report ?


Solution

  • Answering my own question:

    According to https://gcc.gnu.org/ml/gcc-help/2017-03/msg00078.html its indeed a bug in GCC. The temporary is destroyed in the parent and not in the children in a cilk_spawn. So if the thread fork really occur, it might be destroyed too early.