搜索
熱搜: 活動 交友 discuz
查看: 2630|回復: 0
打印 上一主題 下一主題

[教學] C++多執行緒(四)

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-8-14 04:47:56 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
一 互鎖函數

      互鎖函數的家族十分的龐大,可以查看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
= (HANDLE)_beginthreadex( NULL,         // security

0,            // stack size
            ThreadEntity,
            (
void*)&i,           // arg list

0,  
            
&uiThreadID );

        
if ( hth==
0 )
            printf(
"Failed to create thread 1\n");        
    }


    WaitForMultipleObjects( THREAD_MAX、hth,
true,10000);   

   
for(int i =
0; i<THREAD_MAX; ++i)
        CloseHandle( hth );
   
    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 function            Description                 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.         InterlockedCompare64Exchange128Performsan atomic compare-and-exchange operation on the specified values. Thefunction compares the specified 64-bit values and exchanges with thespecified 128-bit value based on the outcome of the comparison.         InterlockedCompare64ExchangeAcquire128Performsan atomic compare-and-exchange operation on the specified values. Thefunction compares the specified 64-bit values and exchanges with thespecified 128-bit value based on the outcome of the comparison. Theoperation is performed with acquire memory access semantics.         InterlockedCompare64ExchangeRelease128Performsan atomic compare-and-exchange operation on the specified values. Thefunction compares the specified 64-bit values and exchanges with thespecified 128-bit value based on the outcome of the comparison. Theoperation is performed with release memory access semantics.         InterlockedCompareExchangePerformsan atomic compare-and-exchange operation on the specified values. Thefunction compares two specified 32-bit values and exchanges withanother 32-bit value based on the outcome of the comparison.         InterlockedCompareExchange64Performsan atomic compare-and-exchange operation on the specified values. Thefunction compares two specified 64-bit values and exchanges withanother 64-bit value based on the outcome of the comparison.         InterlockedCompareExchangeAcquirePerformsan atomic compare-and-exchange operation on the specified values. Thefunction compares two specified 32-bit values and exchanges withanother 32-bit value based on the outcome of the comparison. Theoperation is performed with acquire memory access semantics.         InterlockedCompareExchangeAcquire64Performsan atomic compare-and-exchange operation on the specified values. Thefunction compares two specified 64-bit values and exchanges withanother 64-bit value based on the outcome of the comparison. Theexchange is performed with acquire memory access semantics.         InterlockedCompareExchangePointerPerformsan atomic compare-and-exchange operation on the specified pointervalues. The function compares two specified pointer values andexchanges with another pointer value based on the outcome of thecomparison.         InterlockedCompareExchangePointerAcquirePerformsan atomic compare-and-exchange operation on the specified pointervalues. The function compares two specified pointer values andexchanges with another pointer value based on the outcome of thecomparison. The operation is performed with acquire memory accesssemantics.         InterlockedCompareExchangePointerReleasePerformsan atomic compare-and-exchange operation on the specified pointervalues. The function compares two specified pointer values andexchanges with another pointer value based on the outcome of thecomparison. The operation is performed with release memory accesssemantics.         InterlockedCompareExchangeReleasePerformsan atomic compare-and-exchange operation on the specified values. Thefunction compares two specified 32-bit values and exchanges withanother 32-bit value based on the outcome of the comparison. Theexchange is performed with release memory access semantics.         InterlockedCompareExchangeRelease64Performsan atomic compare-and-exchange operation on the specified values. Thefunction compares two specified 64-bit values and exchanges withanother 64-bit value based on the outcome of the comparison. Theexchange 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 anatomic operation. The operation is performed with acquire memory accesssemantics.         InterlockedDecrementAcquire64Decrements(decreases by one) the value of the specified 64-bit variable as anatomic operation. The operation is performed with acquire memory accesssemantics.         InterlockedDecrementReleaseDecrements(decreases by one) the value of the specified 32-bit variable as anatomic operation. The operation is performed with release memory accesssemantics.         InterlockedDecrementRelease64Decrements(decreases by one) the value of the specified 64-bit variable as anatomic operation. The operation is performed with release memory accesssemantics.         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.         InterlockedExchangeAcquireSetsa 32-bit variable to the specified value as an atomic operation. Theoperation is performed with acquire memory access semantics.         InterlockedExchangeAcquire64Setsa 32-bit variable to the specified value as an atomic operation. Theoperation 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 anatomic operation. The operation is performed using acquire memoryaccess semantics.         InterlockedIncrementAcquire64Increments(increases by one) the value of the specified 64-bit variable as anatomic operation. The operation is performed using acquire memoryaccess semantics.         InterlockedIncrementReleaseIncrements(increases by one) the value of the specified 32-bit variable as anatomic operation. The operation is performed using release memoryaccess semantics.         InterlockedIncrementRelease64Increments(increases by one) the value of the specified 64-bit variable as anatomic operation. The operation is performed using release memoryaccess 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 function            Description                 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.
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2024-4-29 05:28 , Processed in 0.276827 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表