views:

162

answers:

1

I have two piece of C++ code running on 2 different cores. Both of them wirte to the same file. How to use openMP and make sure there is no crash?

A: 

You want the OMP_SET_LOCK/OMP_UNSET_LOCK functions: https://computing.llnl.gov/tutorials/openMP/#OMP_SET_LOCK. Basically:

omp_lock_t writelock;

OMP_INIT_LOCK(&writelock);

#pragma omp parallel for
for ( i = 0; i < x; i++ )
{
    // some stuff
    OMP_SET_LOCK(&writelock);
    // one thread at a time stuff
    OMP_UNSET_LOCK(&writelock);
    // some stuff
}

OMP_DESTROY_LOCK(&writelock);

Most locking routines such as pthreads semaphores and sysv semaphores work on that sort of logic, although the specific API calls are different.

Ninefingers