Question: In my attempt to develope a thread-safe C++ weak pointer template class, I need to check a flag that indicating the object is still alive, if yes then increment the object's reference count and I need to do the both steps atomically.

I know the existance of intrinsics functions provided by the compiler, for instance \_InterlockedCompareExchange() and \_InterlockedIncrement(). But what I want is an interlockedCompareIncrement() function, is there an efficient way to simulate this intrinsic using other primitives, at least on the Windows x86 platform?

Answer: 
If you want your library to run on multiple CPU or multiple core machines you have to use the hardware support provided by the CPU. Here are some references for you:

<http://en.wikipedia.org/wiki/Test-and-set>
<http://software.intel.com/en-us/forums/showthread.php?t=47498>

Or you have to use locking mechanism provided by the OS. Such as

<http://msdn.microsoft.com/en-us/library/ms684841%28VS.85%29.aspx>
or
<http://en.wikipedia.org/wiki/POSIX_Threads>
Since you are in C++, you can write your own assembly code.

Perhaps this is related to [Implement atomic increment using atomic swap?](https://stackoverflow.com/questions/1712863/implement-atomic-increment-using-atomic-swap)