Critical section refers to either a piece of code that will run concurrently in several threads accessing global data or resources (requiring synchronisation), or a user-level spinlock combined with a mutex object under the Windows operating system. A critical section in the latter sense is functionally identical to a mutex that it cannot shared with a different process and that it is several orders of magnitudes faster in the non-congested case.
Questions tagged [critical-section]
529 questions
126
votes
8 answers
What is the difference between atomic and critical in OpenMP?
What is the difference between atomic and critical in OpenMP?
I can do this
#pragma omp atomic
g_qCount++;
but isn't this same as
#pragma omp critical
g_qCount++;
?

codereviewanskquestions
- 13,460
- 29
- 98
- 167
75
votes
2 answers
What is the purpose of the "PAUSE" instruction in x86?
I am trying to create a dumb version of a spin lock. Browsing the web, I came across a assembly instruction called "PAUSE" in x86 which is used to give hint to a processor that a spin-lock is currently running on this CPU. The intel manual and other…

prathmesh.kallurkar
- 5,468
- 8
- 39
- 50
69
votes
5 answers
What is progress and bounded waiting in critical section?
I was reading Critical Section Problem from Operating System Concepts by Peter B. Galvin.
According to it
1) Progress is : If no process is executing in its critical section and some processes wish to enter their critical sections, then only those…

Navneet Srivastava
- 933
- 2
- 9
- 14
37
votes
5 answers
Do I need to lock object when reading from it?
I am writing a program where there is an object shared by multiple threads:
A) Multiple write threads write to the object (all running the same
function)
B) A read thread which accesses the object every 5 seconds
C) A read thread which accesses…

Andy
- 2,770
- 9
- 35
- 42
33
votes
3 answers
How to use lock in OpenMP?
I have two pieces of C++ code running on 2 different cores. Both of them write to the same file.
How to use OpenMP and make sure there is no crash?

MainID
- 29,070
- 19
- 57
- 70
23
votes
7 answers
Is Critical Section always faster?
I was debugging a multi-threaded application and found the internal structure of CRITICAL_SECTION. I found data member LockSemaphore of CRITICAL_SECTION an interesting one.
It looks like LockSemaphore is an auto-reset event (not a semaphore as the…

aJ.
- 34,624
- 22
- 86
- 128
22
votes
3 answers
How to lock on object which shared by multiple async method in nodejs?
I have one object with different properties in nodejs, there are different async function which access and modify that object with some complex execution. A single async function may have internal callbacks (or async functions), that may take some…

Nilesh Wagh
- 940
- 1
- 12
- 26
22
votes
5 answers
Faster TMultiReadExclusiveWriteSynchronizer?
Is there a faster kind of TMultiReadExclusiveWriteSynchronizer out there? FastCode perhaps?
Starting with Windows Vista, Microsoft added a Slim Reader/Writer lock. It performs much better than Delphi's TMultiReadExclusiveWriteSynchronizer.…

Ian Boyd
- 246,734
- 253
- 869
- 1,219
20
votes
6 answers
Confusion about the lock statement in C#
This is from MSDN:
The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section.
Does a critical section have to be same as the critical section?
Or does it mean:
The lock…

Cui Pengfei 崔鹏飞
- 8,017
- 6
- 46
- 87
20
votes
3 answers
Is it valid to nest a critical section?
For example, would this be valid?
CRITICAL_SECTION cs;
::InitializeCriticalSection( &cs );
::EnterCriticalSection( &cs ); // First level
::EnterCriticalSection( &cs ); // Second level
/* do some stuff */
::LeaveCriticalSection( &cs…

Jim Fell
- 13,750
- 36
- 127
- 202
16
votes
8 answers
Difference between "Critical Section", "Critical Region" and "Constrained Execution Region"
Are these actually three different concepts or am I getting jumbled? (I've been reading articles about threading and garbage collection together and have confused myself.)
"Critical section" - I think this may just be the term for sections of code…

J M
- 1,877
- 2
- 20
- 32
16
votes
5 answers
pthreads : pthread_cond_signal() from within critical section
I have the following piece of code in thread A, which blocks using pthread_cond_wait()
pthread_mutex_lock(&my_lock);
if ( false == testCondition )
pthread_cond_wait(&my_wait,&my_lock);
pthread_mutex_unlock(&my_lock);
I have the…

curryage
- 161
- 1
- 1
- 4
14
votes
2 answers
.NET application hangs with GC thread deadlock
We have a problem with our application that is using a mixture of managed (C#) and unmanaged (C++) code. Basically we have a exe that invokes a bunch of assemblies and one of these assemblies is a MC++ wrapper of our C++ library. The application is…

user1210698
- 141
- 3
13
votes
3 answers
Thread safety in C# arrays
Does having 2 different threads :
one reading from a C# array (e.g from first location),
and another one writing to the same C# array but to a different location(e.g to the last location)
is thread safe or not?
(And I mean here without locking…

Betamoo
- 14,964
- 25
- 75
- 109
13
votes
4 answers
Implementing a critical section in CUDA
I'm trying to implement a critical section in CUDA using atomic instructions, but I ran into some trouble. I have created the test program to show the problem:
#include
#include
#include
__global__ void…

John
- 2,012
- 2
- 21
- 33