Site Tools


critical-sections-openmp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
critical-sections-openmp [June 11, 2026 at 09:21] – Ivan Janevskicritical-sections-openmp [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
-# Critical sections (OpenMP) 
-In POSIX threads, mutual exclusion is explicit: you call `pthread_mutex_lock` before a shared update and `pthread_mutex_unlock` after. 
- 
-**Critical sections** in OpenMP do the same thing with a pragma. The enclosed block is protected by an implicit mutex: only one thread at a time executes it, and threads that arrive while another is inside are blocked until it exits. Unlike [[atomic-openmp|`atomic`]], which is limited to a single read-modify-write operation, `critical` can protect an arbitrary sequence of statements. 
- 
-```c 
-#pragma omp parallel for 
-for (int i = 0; i < N; i++) { 
-    int val = expensive_compute(i); 
-    #pragma omp critical 
-    { 
-        if (val > best) { 
-            best = val; 
-            best_idx = i; 
-        } 
-    } 
-} 
-``` 
- 
-Critical sections can be named (`#pragma omp critical(name)`) so that threads only contend with sections sharing the same name. Two differently-named critical sections can be entered simultaneously by different threads. Unnamed sections all share a single implicit lock, so mixing named and unnamed sections in the same program can cause unexpected serialisation. 
  
critical-sections-openmp.1781169669.md.gz · Last modified: by Ivan Janevski