Jump to: navigation, search


1,525 bytes added, 05:27, 3 December 2016
====Mutual Exclusion====
Openmp 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  omp_lock_t lock; omp_init_lock(&lock); int i = 0; #pragma omp parallel num_threads(8) { omp_set_lock(&lock); i++; omp_unset_lock(&lock); } omp_destroy_lock(&lock); A lock is somewhat similar to a critical section as it guarantees that some instructions can only be performed by 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 { i++; } 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. 
=====C++ 11=====

Navigation menu