Gold mine of Visual C++ tricks!
Posts tagged InterlockedIncrement
Interlocked family of functions for simple Synchronizations.
01521 days
![]()
In multithreaded environment we usually use Mutex to synchronize access to resources. But sometimes our resources may be a single integer member variable which holds some count or like that. For such light weight resource, using mutex is not preferred.
![]()
For that you can Interlocked family of functions. Please see some of the functions below.
InterlockedIncrement()
InterlockedDecrement()
InterlockedExchange()
InterlockedCompareExchange()
InterlockedExchangePointer()
…
For example if you need to increment a member variable threadsafely, you can use InterlockedIncrement(). That function will ensure the thread synchronization without any additional kernel objects such as mutex etc… Please see the code block below.
long m_lCount = 0;
...
// Increment the value.
InterlockedIncrement( &m_lCount );
InterlockedExchangePointer()