can someone help explain the different between interlocked variable access AND critical sections interlocked increment in c++? thanks, much appreciated, in advance.
-
2C++ has no concept of "interlocked variables" or "critical sections". To which specific library are you referring? – ildjarn Jun 24 '11 at 00:35
-
1sorry, looking at http://msdn.microsoft.com/en-us/library/ms684122%28v=vs.85%29.aspx compared to critical sections when doing windows threading – Beef Jun 24 '11 at 00:39
-
@ildjarn: Probably the Windows API. – Billy ONeal Jun 24 '11 at 00:39
-
1But even in Windows API, there is no such concept as `critical sections interlocked increment`. There are critical sections, and there are interlocked operations. – Remus Rusanu Jun 24 '11 at 00:41
-
1@Remus: True -- I think the OP means "What's the difference between critical sections and the interlockedXxx methods" -- but I agree the question is worded funny. – Billy ONeal Jun 24 '11 at 00:44
3 Answers
Basically, all those InterlockedXXX
functions are more or less intrinsics that map to relatively few (typically one) assembly instructions. Such an operation cannot be interrupted and is thus said to be atomic (the atomicity is achieved at CPU level, at least if this is possible on the target platform).
A CRITICAL_SECTION
is a synchronization primitive that can protect longer sections. It really does a lock and competing threads will be forced to wait until a thread releases ownership of the critical section.
Critical sections are OS primitives, but they are limited to a single process. Their big brother of a critical section under Windows is a Mutex
, which can be used for cross-process synchronization.
Use the InterlockedXXX
functions if you can (for example it makes no sense to use a full critical section object to protect a single counter). You may want to have a look at the various prototypes and their usage upfront. Many people use critical sections where a InterlockedCompareExchange
would do ...

- 45,603
- 7
- 82
- 122
-
The Interlocked functions are not intrinsics -- indeed, they can be called from languages other than C++. However, they are implemented in terms of CPU specific synchronization instructions. – Billy ONeal Jun 24 '11 at 02:57
-
To be complete, Visual C++ *does* provide compiler intrinsic versions of the interlocked suite of functions. Prefix the function with an underscore and you get the intrinsic version. – aalpern Feb 09 '12 at 05:10
A critical section is a lock. An InterlockedXxx
function call is an atomic operation -- no lock. You could build either in terms of the other, but in practice they have very different performance characteristics (Interlocked functions are usually faster).

- 104,103
- 58
- 317
- 552
Basically, the Interlocked functions are the same concept as a critical section, but they're implemented in hardware for certain operations - lock, operate, unlock. This can make them much faster, but limits their applicability. Critical sections are much more generic, but the relative cost of using one is higher. They also have problems like race conditions and deadlocks.

- 144,682
- 38
- 256
- 465