Interlocked family of functions for simple Synchronizations.

24 03 2008

Icon Description
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.

Icon How Can I Do It?
For that you can Interlocked family of functions. Please see some of the functions below.


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 );