I can't understand what values the function std::barrier sync_point takes

35 Views Asked by At

I'm trying to transmit the array bounds element values to the function array_sync. The code:

auto array_sync = []() noexcept {
    static auto phase = bounds[i];
    return phase;
    phase = bounds[i+1];
};
std::barrier sync_point(bounds, on_completion);

auto array_work = [&](std::string name) {
    array_transform(bounds[i + 1] * n, bounds[i + 2] * n, num_array_1, n);
    sync_point.arrive_and_wait();
    array_transform(bounds[i + 1] * n, bounds[i + 2] * n, num_array_1, n);
    sync_point.arrive_and_wait();
};
// the cycle for threads output
for (int i = 0; i <= 6; i += 2) {
    // the function for main thread
    array_transform(bounds[i + 1] * n, bounds[i + 2] * n, num_array_1, n);
    // new thread
    threads.emplace_back(work, bounds);
}
// threads joining
for (auto& thread : threads) {
    thread.join();
}

Tell me please, how to declare the function sync_point right.

1

There are 1 best solutions below

0
Wintermute On

If I decipher what you're trying to do correctly, then

  auto f = [&, phase=bounds[i]]() mutable noexcept {
    auto result = phase;
    phase = bounds[i+1];
    return result;
  };

should do what you want. The way this works is:

  1. [&] means that it captures the surroundings by reference, so you can use bounds and i and their values change in the lambda when they do on the outside
  2. [phase=bounds[i]] gives the lambda object a named member phase that's initialized with bounds[i]
  3. mutable makes it so that members of the lambda are changeable, so you can update phase in the lambda function body.
  4. result is introduced because the function exits at return, so you have to change phase before then.

It's a bit hard to tell if this is what you want, though, because the code you showed never calls array_sync.