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.

496 questions
114
votes
7 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
  • 11,772
  • 25
  • 93
  • 158
63
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
  • 4,930
  • 8
  • 32
  • 48
55
votes
4 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…
29
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
  • 25,722
  • 19
  • 53
  • 70
26
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,510
  • 7
  • 33
  • 40
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.
  • 32,074
  • 21
  • 79
  • 124
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
  • 220,884
  • 228
  • 805
  • 1,125
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 崔鹏飞
  • 7,124
  • 4
  • 38
  • 79
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
  • 12,390
  • 29
  • 117
  • 192
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
15
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,739
  • 2
  • 19
  • 31
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…
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
  • 12,574
  • 22
  • 69
  • 107
13
votes
3 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
  • 1,952
  • 2
  • 20
  • 33
12
votes
2 answers

Critical section in MPI?

I have some code to print a 2D array to the standard output. The problem is that when I run it, every process writes to the output and the data overlaps, rendering it unusable. How can i build a critical section in MPI so that only one process at a…
alexsardan
  • 253
  • 1
  • 3
  • 7
1
2 3
33 34