C++多线程-互锁

多线程同步之WIN API互锁函数 ( 可以避免使用CriticalSection或Mutex) 一 互锁函数       互锁函数的家族十分的庞大,可以查看msdn( http://msdn2.microsoft.com/en-us/library/ms686360.aspx)以InterLocked开始的函数都是户数函数。使用互锁函数的优点是:他的速度要比其他的CriticalSection,Mutex,Event,Semaphore快很多。 二 简单实例       使用一些实例说明部分互锁函数的使用: 1) LONG InterlockedExchangeAdd(   PLONG plAddend,LONG Increment); 简单实例,在线程函数中对全局的变量自增,在开始使其为0,在线程都执行完以后输出全局变量的值,如果我们不使用互锁函数,则最后输出的结果,大部分情况是不正确的,比如我们一共有10000个线程的话,则全局变量的值一般是比10000要小;但是如果我们使用互锁函数来实现自增,则就快速的实现了线程安全,最后输出的全局变量一定是10000.
#include  < windows.h > #include  < process.h > #include  < stdio.h > #define  THREAD_MAX 100000 int  g_x  =   0 ; unsigned  __stdcall ThreadEntity( void   *  pVoid) {     g_x++;     //InterlockedExchangeAdd(reinterpret_cast<long*>(&g_x),1);         return 1;         } int  main() {          HANDLE   hth[THREAD_MAX];     unsigned  uiThreadID[THREAD_MAX];     printf("start create children threadings:/n");     for(int i = 0; i < THREAD_MAX; ++i)     {         hth[i] = (HANDLE)_beginthreadex( NULL,         // security             0,            // stack size             ThreadEntity,             (void*)&i,           // arg list             0,               &uiThreadID[i] );         if ( hth[i]== 0 )             printf("Failed to create thread 1/n");             }     WaitForMultipleObjects( THREAD_MAX, hth,true,10000);        for(int i = 0; i<THREAD_MAX; ++i)         CloseHandle( hth[i] );          printf("last: g_x is %d/n",g_x);     printf("Primary thread terminating./n"); }
在上面的代码中使用了 //InterlockedExchangeAdd(reinterpret_cast<long*>(&g_x),1); 来实现g_x的线程安全的自增。   2)使用LONG InterlockedExchange(PLONG plTarget,LONG lValue);实现循环锁:
//  Global variable indicating whether a shared resource is in use or not BOOL g_fResourceInUse  =  FALSE; void  Func1()  {    //Wait to access the resource.    while(InterlockedExchange(&g_fResourceInUse, TRUE) == TRUE)       Sleep(0);    //Access the resource.        //We no longer need to access the resource.    InterlockedExchange(&g_fResourceInUse, FALSE); }
  3)太多了,不举了,以后用到了再放到这里把。 三 互锁函数列表 一般的互锁函数:
Interlocked functionDescription
InterlockedAddPerforms an atomic addition operation on the specified LONG values.
InterlockedAdd64Performs an atomic addition operation on the specified LONGLONG values.
InterlockedAddAcquirePerforms an atomic addition operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedAddAcquire64Performs an atomic addition operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedAddReleasePerforms an atomic addition operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedAddRelease64Performs an atomic addition operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
InterlockedAndPerforms an atomic AND operation on the specified LONG values.
InterlockedAndAcquirePerforms an atomic AND operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedAndReleasePerforms an atomic AND operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedAnd8Performs an atomic AND operation on the specified char values.
InterlockedAnd8AcquirePerforms an atomic AND operation on the specified char values. The operation is performed with acquire memory access semantics.
InterlockedAnd8ReleasePerforms an atomic AND operation on the specified char values. The operation is performed with release memory access semantics.
InterlockedAnd16Performs an atomic AND operation on the specified SHORT values.
InterlockedAnd16AcquirePerforms an atomic AND operation on the specified SHORT values. The operation is performed with acquire memory access semantics.
InterlockedAnd16ReleasePerforms an atomic AND operation on the specified SHORT values. The operation is performed with release memory access semantics.
InterlockedAnd64Performs an atomic AND operation on the specified LONGLONG values.
InterlockedAnd64AcquirePerforms an atomic AND operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedAnd64ReleasePerforms an atomic AND operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
InterlockedBitTestAndResetTests the specified bit of the specified LONG value and sets it to 0.
InterlockedBitTestAndReset64Tests the specified bit of the specified LONG64 value and sets it to 0.
InterlockedBitTestAndSetTests the specified bit of the specified LONG value and sets it to 1.
InterlockedBitTestAndSet64Tests the specified bit of the specified LONG64 value and sets it to 1.
InterlockedCompare64Exchange128Performs an atomic compare-and-exchange operation on the specified values. The function compares the specified 64-bit values and exchanges with the specified 128-bit value based on the outcome of the comparison.
InterlockedCompare64ExchangeAcquire128Performs an atomic compare-and-exchange operation on the specified values. The function compares the specified 64-bit values and exchanges with the specified 128-bit value based on the outcome of the comparison. The operation is performed with acquire memory access semantics.
InterlockedCompare64ExchangeRelease128Performs an atomic compare-and-exchange operation on the specified values. The function compares the specified 64-bit values and exchanges with the specified 128-bit value based on the outcome of the comparison. The operation is performed with release memory access semantics.
InterlockedCompareExchangePerforms 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.
InterlockedCompareExchange64Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 64-bit values and exchanges with another 64-bit value based on the outcome of the comparison.
InterlockedCompareExchangeAcquirePerforms 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. The operation is performed with acquire memory access semantics.
InterlockedCompareExchangeAcquire64Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 64-bit values and exchanges with another 64-bit value based on the outcome of the comparison. The exchange is performed with acquire memory access semantics.
InterlockedCompareExchangePointerPerforms an atomic compare-and-exchange operation on the specified pointer values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison.
InterlockedCompareExchangePointerAcquirePerforms an atomic compare-and-exchange operation on the specified pointer values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison. The operation is performed with acquire memory access semantics.
InterlockedCompareExchangePointerReleasePerforms an atomic compare-and-exchange operation on the specified pointer values. The function compares two specified pointer values and exchanges with another pointer value based on the outcome of the comparison. The operation is performed with release memory access semantics.
InterlockedCompareExchangeReleasePerforms 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. The exchange is performed with release memory access semantics.
InterlockedCompareExchangeRelease64Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 64-bit values and exchanges with another 64-bit value based on the outcome of the comparison. The exchange is performed with release memory access semantics.
InterlockedDecrementDecrements (decreases by one) the value of the specified 32-bit variable as an atomic operation.
InterlockedDecrement64Decrements (decreases by one) the value of the specified 64-bit variable as an atomic operation.
InterlockedDecrementAcquireDecrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedDecrementAcquire64Decrements (decreases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedDecrementReleaseDecrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed with release memory access semantics.
InterlockedDecrementRelease64Decrements (decreases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed with release memory access semantics.
InterlockedExchangeSets a 32-bit variable to the specified value as an atomic operation.
InterlockedExchange64Sets a 64-bit variable to the specified value as an atomic operation.
InterlockedExchangeAcquireSets a 32-bit variable to the specified value as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedExchangeAcquire64Sets a 32-bit variable to the specified value as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedExchangeAddPerforms an atomic addition of two 32-bit values.
InterlockedExchangeAdd64Performs an atomic addition of two 64-bit values.
InterlockedExchangeAddAcquirePerforms an atomic addition of two 32-bit values. The operation is performed with acquire memory access semantics.
InterlockedExchangeAddAcquire64Performs an atomic addition of two 64-bit values. The operation is performed with acquire memory access semantics.
InterlockedExchangeAddReleasePerforms an atomic addition of two 32-bit values. The operation is performed with release memory access semantics.
InterlockedExchangeAddRelease64Performs an atomic addition of two 64-bit values. The operation is performed with release memory access semantics.
InterlockedExchangePointerAtomically exchanges a pair of pointer values.
InterlockedExchangePointerAcquireAtomically exchanges a pair of pointer values. The operation is performed with acquire memory access semantics.
InterlockedIncrementIncrements (increases by one) the value of the specified 32-bit variable as an atomic operation.
InterlockedIncrement64Increments (increases by one) the value of the specified 64-bit variable as an atomic operation.
InterlockedIncrementAcquireIncrements (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed using acquire memory access semantics.
InterlockedIncrementAcquire64Increments (increases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed using acquire memory access semantics.
InterlockedIncrementReleaseIncrements (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed using release memory access semantics.
InterlockedIncrementRelease64Increments (increases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed using release memory access semantics.
InterlockedOrPerforms an atomic OR operation on the specified LONG values.
InterlockedOrAcquirePerforms an atomic OR operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedOrReleasePerforms an atomic OR operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedOr8Performs an atomic OR operation on the specified char values.
InterlockedOr8AcquirePerforms an atomic OR operation on the specified char values. The operation is performed with acquire memory access semantics.
InterlockedOr8ReleasePerforms an atomic OR operation on the specified char values. The operation is performed with release memory access semantics.
InterlockedOr16Performs an atomic OR operation on the specified SHORT values.
InterlockedOr16AcquirePerforms an atomic OR operation on the specified SHORT values. The operation is performed with acquire memory access semantics.
InterlockedOr16ReleasePerforms an atomic OR operation on the specified SHORT values. The operation is performed with release memory access semantics.
InterlockedOr64Performs an atomic OR operation on the specified LONGLONG values.
InterlockedOr64AcquirePerforms an atomic OR operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedOr64ReleasePerforms an atomic OR operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
InterlockedXorPerforms an atomic XOR operation on the specified LONG values.
InterlockedXorAcquirePerforms an atomic XOR operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedXorReleasePerforms an atomic XOR operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedXor8Performs an atomic XOR operation on the specified char values.
InterlockedXor8AcquirePerforms an atomic XOR operation on the specified char values. The operation is performed with acquire memory access semantics.
InterlockedXor8ReleasePerforms an atomic XOR operation on the specified char values. The operation is performed with release memory access semantics.
InterlockedXor16Performs an atomic XOR operation on the specified SHORT values.
InterlockedXor16AcquirePerforms an atomic XOR operation on the specified SHORT values. The operation is performed with acquire memory access semantics.
InterlockedXor16ReleasePerforms an atomic XOR operation on the specified SHORT values. The operation is performed with release memory access semantics.
InterlockedXor64Performs an atomic XOR operation on the specified LONGLONG values.
InterlockedXor64AcquirePerforms an atomic XOR operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedXor64ReleasePerforms an atomic XOR operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
链表的互锁函数:
Singly-linked list functionDescription
InitializeSListHeadInitializes the head of a singly linked list.
InterlockedFlushSListFlushes the entire list of items in a singly linked list.
InterlockedPopEntrySListRemoves an item from the front of a singly linked list.
InterlockedPushEntrySListInserts an item at the front of a singly linked list.
QueryDepthSListRetrieves the number of entries in the specified singly linked list.
RtlFirstEntrySListRetrieves the first entry in a singly linked list.
RtlInitializeSListHeadInitializes the head of a singly linked list. Applications should call InitializeSListHead instead.
RtlInterlockedFlushSListFlushes the entire list of items in a singly linked list. Applications should call InterlockedFlushSList instead.
RtlInterlockedPopEntrySListRemoves an item from the front of a singly linked list. Applications should call InterlockedPopEntrySList instead.
RtlInterlockedPushEntrySListInserts an item at the front of a singly linked list. Applications should call InterlockedPushEntrySList instead.
RtlQueryDepthSListRetrieves the number of entries in the specified singly linked list. Applications should call QueryDepthSList instead.
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值