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
8
votes
2 answers
Under what circumstances might a Windows Critical Section have a negative Lock Count?
Is there any circumstance in which the LockCount field of a RTL_CRITICAL_SECTION structure in Windows can legitimately be negative?
We're tracking a VERY elusive crash and one symptom we're seeing is a CS with a negative LockCount. At the time of…

Wilson F
- 1,250
- 1
- 20
- 37
8
votes
1 answer
What will be the critical section code for a shared queue accessed by two threads?
Suppose we have a shared queue (implemented using an array), which two threads can access, one for reading data from it, and other for writing data to it. Now, I have a problem of synchronization. I'm implementing this using Win32 API's…

Rajendra Uppal
- 19,218
- 15
- 59
- 57
8
votes
1 answer
Why is std::mutex twice as slow as CRITICAL_SECTION
std::mutex is implemented with critical sections, which is why it's much faster than OS Mutex (on Windows). However it's not as fast as a Windows CRITICAL_SECTION.
Timings just a tight loop in a single thread:
423.76ns ATL CMutex
41.74ns…

Philip
- 1,691
- 4
- 21
- 41
8
votes
3 answers
Critical Section in JavaScript or jQuery
I have a webpage, in which a certain Ajax event is triggered asynchronously. This Ajax section could be called once or more than once. I do not have control over the number of times this event is triggered, nor the timing.
Also, there is a certain…

Greeso
- 7,544
- 9
- 51
- 77
8
votes
2 answers
Cost of mutex,critical section etc on Windows
I read somewhere that the overhead of a mutex is not that much, because the context switching only happens in case of contention.
Also known Futexes in Linux.
Does the same thing hold good in Windows? Is Critical Section a more apt map to mutexes in…

Desert Ice
- 4,461
- 5
- 31
- 58
8
votes
1 answer
Understanding TCriticalSection and Synchronize
I would like to confirm here if I understood correctly how TCriticalSection and Synchronize operate.
As far as I know right now Synchronize uses SendMessage (update: or at least used it in older VCL versions as mentioned in couple of comments below)…

Coder12345
- 3,431
- 3
- 33
- 73
8
votes
5 answers
Multithreading. Do I need critical sections for read-only access?
I have a bunch of threads. They should access a singleton containing configuration data which is initialized once when the singleton is created. Hence on the first access. So further actions on the singleton are just read-only. Do I need critical…

ManuelSchneid3r
- 15,850
- 12
- 65
- 103
7
votes
1 answer
Why is my thread blocked by a critical section not being held by anything?
I am having an issue with a critical section in C++. I'm getting a hung window and when I dump the process I can see the thread waiting on a critical section:
16 Id: b10.b88 Suspend: 1 Teb: 7ffae000 Unfrozen
ChildEBP RetAddr
0470f158 7c90df3c…

dlanod
- 8,664
- 8
- 54
- 96
7
votes
3 answers
What exactly is a critical section?
Just want a little clarity on the this.
Imagine I use the windows api of EnterCriticalSection. I call all of them with EnterCriticalSection(&criticalsection);
This is the thread function that is multi threaded
void thread (){
//enter critical…

Jake
- 2,877
- 8
- 43
- 62
7
votes
1 answer
InitializeCriticalSectionAndSpinCount, is there a default Spin Count?
The API method InitializeCriticalSectionAndSpinCount allows you to set a spin count so when EnterCriticalSection is called, it loops using a spinlock to try to acquire the resource some number of times. Only if all the attempts fail does the thread…

Darian Miller
- 7,808
- 3
- 43
- 62
7
votes
6 answers
Problems using EnterCriticalSection
I need to work with array from several threads, so I use CRITICAL SECTION to give it an exclusive access to the data.
Here is my template:
#include "stdafx.h"
#ifndef SHAREDVECTOR_H
#define SHAREDVECTOR_H
#include
#include…

chester89
- 8,328
- 17
- 68
- 113
7
votes
1 answer
Robust CRITCAL_SECTION for shared memory?
We have some data structures that we are sharing across processes on Windows. (Via a shared data segment in a DLL that's loaded by all these processes.)
We need to synchronize some accesses and we measured that the performance hit of using a Win32…

Martin Ba
- 37,187
- 33
- 183
- 337
7
votes
2 answers
Why is the OwningThread member of CRITICAL_SECTION of type HANDLE, when it is denoting the thread ID?
I'm trying to add some debug checking for a CRITICAL_SECTION unlocking code, and I tried the following:
...
if (m_pCritSect) {
ASSERT(m_pCritSect->OwningThread == GetCurrentThreadId());
LeaveCriticalSection(m_pCritSect);
}
}
From…

Martin Ba
- 37,187
- 33
- 183
- 337
7
votes
2 answers
Critical Sections and return values in C++
In attempting to create a thread-safe container class from scratch, I've run into the problem of returning values from access methods. For example in Windows:
myNode getSomeData( )
{
EnterCriticalSection(& myCritSec);
myNode retobj;
// fill…

ThomasMcLeod
- 7,603
- 4
- 42
- 80
7
votes
1 answer
What are Critical sections in threads
I was reading about mutex,semaphores and critical sections. I understand that mutex synchronizes a resource so that only one thread accesses it at a time a semaphore allows a specific no of threads to access a resource but what do critical sections…

Rajeshwar
- 11,179
- 26
- 86
- 158