Harb
Harb

Reputation: 33

OpenMP filling array with two threads in series

I have an array. And I need to fill it with two threads each value consequently, using omp_set_lock, and omp_unset_lock. First thread should write first value, then second array should write second value etc. I have no idea how to do that, because, in openmp you cant't explicitly make one thread wait for another. Have any ideas?

Upvotes: 1

Views: 394

Answers (1)

dspfnder
dspfnder

Reputation: 1123

Why not try the omp_set_lock/omp_unset_lock functions?

omp_lock_t lock;

omp_init_lock(&lock);

#pragma omp parallel for

bool thread1 = true;
for (int i = 0; i < arr.size(); ++i) {
    omp_set_lock(&lock);
    if (thread1 == true) {
        arr[i] = fromThread1();
        thread1 = false;
    } else {
        arr[i] = fromThread2();
        thread1 = true;
    }
    omp_unset_lock(&lock);
}

Upvotes: 1

Related Questions