Open main menu
← Older edit
Newer edit →
1,525 bytes added
05:27, 3 December 2016
Handles Mutual Exclusion
offers multiple solutions for handling mutual exclusion.
Scoped Locking may be implemented using the omp_set_lock and omp_unset_lock template functions to allow thread blocking.
Example of Scoped Locking
int i = 0;
#pragma omp parallel num_threads(8)
A lock is somewhat similar to a critical section as it guarantees that some instructions can only be performed
one process at a time. With a lock you make sure that some data elements can only be touched by one process at a time.
Openmp offers a easier solution for mutual exclusion and preventing race conditions within its section constructs as the programmer does not have to worry about initializing and destroying locks.
* critical - region to be executed by only one thread at a time
* atomic - the memory location to be updated by one thread at a time
A critical section works by acquiring a lock, which carries a substantial overhead. Furthermore. If a thread is in one critical section, the other ones are all blocked.
A critical region can by implemented as follows
#pragma omp critical
A atomic region is implemented just as critical region, only the critical construct is replaced by an atomic construct. An atomic section has much lower overhead then a critical section as it does not require locking and unlocking operations as it takes advantage of the hardware providing atomic increment operations.
Danylo Daniel Medinski
Retrieved from "