win32 API InterlockedCompareExchange

本文详细介绍了InterlockedCompareExchange函数的功能及使用方法。此函数通过原子方式比较并交换指定的32位值,适用于多线程环境下的同步操作。文章还解释了函数参数的意义以及返回值的含义。

函数原型:

LONG __cdecl InterlockedCompareExchange(
  __in_out      LONG volatile* Destination,
  __in          LONG Exchange,
  __in          LONG Comparand
);

MSDN上的解释:

Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison.

 

我的理解:

第一个参数和第三个参数比较,如果相等,则将第一个参数的值设为第个参数的值.

 

Return Value

The function returns the initial value of the Destination parameter.

 

返回的是第一个参数的初始值

将原始 Windows 代码重构为跨平台实现,保留 补全 修改 Windows 兼容性同时支持 Linux 编译 #if defined (_MSC_VER) && (_MSC_VER >= 1000) #pragma once #endif #ifndef _HEADER_BASETHREAD #define _HEADER_BASETHREAD #include "Config.h" #include "Semaphore.h" #include <assert.h> //#include "BaseLibary/PoolT.h" #define MUITLEVENT_WAITFOREVER -1 #define MUITLEVENT_WAIT_TIMEOUT 1 #define IS_TIMEOUT(ret) (ret == WAIT_TIMEOUT) /** *线程支撑相关命名空间,包括线程控制,互斥等 */ namespace Thread { typedef void (*FNRunThread)(void*);//返回false则停止运行 /** *基本的线程控制处理 */ class BaseThread; /** *简单的互斥控制 */ class Mutex; //Win32平台 //简单互斥,同一线程也不可重入 class _Foundation_Export_ Mutex { private: #ifdef _DEBUG bool _isLocked; #endif #ifdef _WIN32 mutable volatile long _mutex; #else mutable pthread_mutex_t _mutex; #endif public: Mutex() #ifdef _DEBUG :_isLocked(false) #endif { #ifdef _WIN32 _mutex= 0 ; #else #ifdef NDEBUG int rc = pthread_mutex_init(&_mutex, 0); #else int rc; #if defined(__linux) && !defined(__USE_UNIX98) const pthread_mutexattr_t attr = { PTHREAD_MUTEX_ERRORCHECK_NP }; #else pthread_mutexattr_t attr; rc = pthread_mutexattr_init(&attr); assert(rc == 0); rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); assert(rc == 0); #endif//defined(__linux) && !defined(__USE_UNIX98) rc = pthread_mutex_init(&_mutex, &attr); #if defined(__linux) && !defined(__USE_UNIX98) // Nothing to do #else//(__linux) && !defined(__USE_UNIX98) rc = pthread_mutexattr_destroy(&attr); assert(rc == 0); #endif//defined(__linux) && !defined(__USE_UNIX98) #endif//NDEBUG #endif//_WIN32 } ~Mutex() { #ifdef _WIN32 //do nothing #else int rc = 0; rc = pthread_mutex_destroy(&_mutex); assert(rc == 0); #endif } long Lock(void) { #ifdef _WIN32 while( InterlockedCompareExchange(&_mutex,1,0) != 0) { Sleep( 1 ); } #ifdef _DEBUG if (_isLocked) { //assert(false); } _isLocked=true; #endif return 0; #else return pthread_mutex_lock(&_mutex); #endif } bool TryLock(void) { #ifdef _WIN32 if( InterlockedCompareExchange(&_mutex,1,0) != 0) { return false; } else { #ifdef _DEBUG _isLocked=true; #endif return true; } #else int rc = pthread_mutex_trylock(&_mutex); if(rc != 0 && rc != EBUSY) { //Error } return (rc == 0); #endif } void Unlock(void) { #ifdef _WIN32 InterlockedExchange(&_mutex,0); #else pthread_mutex_unlock(&_mutex); #endif #ifdef _DEBUG _isLocked=false; #endif return; } struct SLock { public: SLock(Mutex& mutex) :_mutex(mutex) { _mutex.Lock(); } ~SLock() { _mutex.Unlock(); } Mutex& _mutex; }; }; #ifdef _WIN32 /** *多线程安全的数字 */ class _Foundation_Export_ InterlockedNumber { public: InterlockedNumber(void) :_number(0) { } InterlockedNumber(long i) :_number(0) { InterlockedExchange(&_number,i); } void operator ++(int) { InterlockedIncrement(&_number); } void operator --(int) { InterlockedDecrement(&_number); } operator long () { return _number; } void Add(long value) { InterlockedExchangeAdd(&_number,value); } const InterlockedNumber& operator = (const InterlockedNumber & rhs) { InterlockedExchange(&_number,rhs._number); return *this; } private: volatile long _number; }; #else /** *多线程安全的数字 */ class InterlockedNumber { public: InterlockedNumber(void) :_number(0) { } operator ++() { _number++; } operator --() { _number--; } operator long () { return _number; } private: volatile long _number; }; #endif//WIN32 /** *基本线程处理 */ class _Foundation_Export_ BaseThread { public: BaseThread(void); ~BaseThread(void); BaseThread& operator = (const BaseThread& rhs); //!启动线程 void StartThread(FNRunThread,void*); //!等待线程结束 bool WaitForEnd(void); //!挂起线程,只能在线程函数中调用 void Suspend(void); //!恢复线程,可以在外部调用 void Resume(void); //!强制结束线程 void ForceKill(void); private: #ifdef _WIN32 typedef void* ThreadSysHandle; #else typedef pthread_t ThreadSysHandle; #endif struct ThreadData { ThreadData() :_end(false) ,_param(0) ,_pfnThread(0) {} bool _end; void* _param; FNRunThread _pfnThread; ThreadSysHandle _h; }; ThreadData* _data; //!模拟挂起所使用的互斥变量 Mutex* _simSuspend; //!释放资源 void SafeRelease(void); //!线程运行函数 #ifdef _WIN32 static DWORD WINAPI ProcessThread(void* param); #else static void* ProcessThread(void* param); #endif }; } #endif//_Header
最新发布
09-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值