Questions tagged [critical-section]

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.

529 questions
6
votes
2 answers

"Pausing" A Thread With A Property

I have a TThread object and want to be able to start/stop the thread via a button on the main form of the program. I've been looking into ways to do this and so far I have the following ideas: Terminate and Free the thread when the user clicks stop…
user1970794
  • 253
  • 3
  • 16
6
votes
3 answers

Why do the threads run serially in this console application?

I'm creating an console application which needs to run several threads in order to accomplish a task. My problem is that threads are running one after another (thread1 start -> work -> end and ONLY then start thread2) instead of running all in the…
RBA
  • 12,337
  • 16
  • 79
  • 126
6
votes
3 answers

How can I implement a thread-safe list wrapper in Delphi?

I have a list wrapper that maintains two Tstringlists and a TClassList I need this to be thread safe, such that: Concurrent writes are not allowed (wait state of some sort should be entered) Reading while writing (or vice versa) is not allowed…
Graza
  • 5,010
  • 6
  • 32
  • 37
6
votes
2 answers

using dispatch_sync as a mutex lock

Here is what I need to do. I hope dispatch_sync would be the best way to do it using GCD I have a certain piece of critical section code that is placed in the applicationDidBecomeActive callback in Appdelegate.. I am wrapping up that method inside a…
6
votes
1 answer

Do I need a fence or barrier or something when mutex locks/unlocks are buried deep in function calls?

I recently learned that compilers will optimize your code by rearranging instructions, and that this can be controlled by using barriers. IIRC, locking a mutex makes a barrier, and unlocking a mutex also makes a barrier, to keep code inside the…
Verdagon
  • 2,456
  • 3
  • 22
  • 36
6
votes
2 answers

Can a Windows CRITICAL_SECTION object be configured to deny recursive access?

By default, a CRITICAL_SECTION object is recursive. Can this behaviour be configured like a pthread mutex to enable or disable recursive thread access? To clarify in response to the comments: I am referring specifically to a Windows CRITICAL_SECTION…
x-x
  • 7,287
  • 9
  • 51
  • 78
6
votes
1 answer

What happen if 2 threads do EnterCriticalSection and thread 1 do DeleteCriticalSection

I am hunting a possible deadlock in my program and im suspecting the following. What happen if 2 threads at the same time calls EnterCriticalSection and thread #1 calls DeleteCriticalSection right after entering, what happen to thread #2 which is…
user1727542
  • 113
  • 2
  • 9
5
votes
3 answers

Critical Sections leaking memory on Vista/Win2008?

It seems that using Critical Sections quite a bit in Vista/Windows Server 2008 leads to the OS not fully regaining the memory. We found this problem with a Delphi application and it is clearly because of using the CS API. (see this SO question) …
5
votes
3 answers

VC++ 2010: Weird Critical Section error

My program is randomly crashing in a small scenario I can reproduce, but it happens in mlock.c (which is a VC++ runtime file) from ntdll.dll, and I can't see the stack trace. I do know that it happens in one of my thread functions, though. This is…
dario_ramos
  • 7,118
  • 9
  • 61
  • 108
5
votes
6 answers

C++: Concurrency and destructors

Suppose you have an object which can be accesed by many threads. A critical section is used to protect the sensitive areas. But what about the destructor? Even if I enter a critical section as soon as I enter the destructor, once the destructor has…
dario_ramos
  • 7,118
  • 9
  • 61
  • 108
5
votes
5 answers

Fair critical section (Linux)

On a multi-threaded Linux application I use a mutex for critical sections. This works very well except for the fairness issue. It can happen that a thread leaving a critical section and re-entering right away does not give any other thread a chance.…
Frank
  • 51
  • 1
  • 2
5
votes
3 answers

When should I use critical sections?

Here's the deal. My app has a lot of threads that do the same thing - read specific data from huge files(>2gb), parse the data and eventually write to that file. Problem is that sometimes it could happen that one thread reads X from file A and…
pop32
  • 165
  • 3
  • 8
5
votes
1 answer

Why did CRITICAL_SECTION performance become worse on Win8

It seems like CRITICAL_SECTION performance became worse on Windows 8 and higher. (see graphs below) The test is pretty simple: some concurrent threads do 3 million locks each to access a variable exclusively. You can find the C++ program at the…
Rom098
  • 2,445
  • 4
  • 35
  • 52
5
votes
4 answers

Does Mutex call a system call?

CRITICAL_SECTION locking (enter) and unlocking (leave) are efficient because CS testing is performed in user space without making the kernel system call that a mutex makes. Unlocking is performed entirely in user space, whereas ReleaseMutex…
Benjamin
  • 10,085
  • 19
  • 80
  • 130
5
votes
1 answer

Unable to enter critical section

Why is it imposible to enter critical section without Sleep(1)? type TMyThread = class(TThread) public procedure Execute; override; end; var T: TMyThread; c: TRTLCriticalSection; implementation procedure TForm1.FormCreate(Sender:…