匯編實現(可內嵌在C++裡使用)
[cpp]
#ifdef WIN32
void __inline atomic_inc(int* v)
{
__asm
{
mov ebx, v
lock inc dword ptr [ebx]
}
}
#else
static __inline__ void atomic_inc(int *v)
{
__asm__ __volatile__(
"lock incl %0"
:"+m" (*v));
}
#endif
C++實現(WIN平台)
[cpp]
LONG
WINAPI
InterlockedIncrement (
__inout LONG volatile *lpAddend
);
ACE實現(跨平台)
[cpp]
ACE_Atomic_Op<ACE_Thread_Mutex,int> index_;//定義
[cpp] view plaincopyprint?
index++;//增一
[cpp] view plaincopyprint?
index.value();//取值