青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

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 function Description
InterlockedAdd Performs an atomic addition operation on the specified LONG values.
InterlockedAdd64 Performs an atomic addition operation on the specified LONGLONG values.
InterlockedAddAcquire Performs an atomic addition operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedAddAcquire64 Performs an atomic addition operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedAddRelease Performs an atomic addition operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedAddRelease64 Performs an atomic addition operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
InterlockedAnd Performs an atomic AND operation on the specified LONG values.
InterlockedAndAcquire Performs an atomic AND operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedAndRelease Performs an atomic AND operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedAnd8 Performs an atomic AND operation on the specified char values.
InterlockedAnd8Acquire Performs an atomic AND operation on the specified char values. The operation is performed with acquire memory access semantics.
InterlockedAnd8Release Performs an atomic AND operation on the specified char values. The operation is performed with release memory access semantics.
InterlockedAnd16 Performs an atomic AND operation on the specified SHORT values.
InterlockedAnd16Acquire Performs an atomic AND operation on the specified SHORT values. The operation is performed with acquire memory access semantics.
InterlockedAnd16Release Performs an atomic AND operation on the specified SHORT values. The operation is performed with release memory access semantics.
InterlockedAnd64 Performs an atomic AND operation on the specified LONGLONG values.
InterlockedAnd64Acquire Performs an atomic AND operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedAnd64Release Performs an atomic AND operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
InterlockedBitTestAndReset Tests the specified bit of the specified LONG value and sets it to 0.
InterlockedBitTestAndReset64 Tests the specified bit of the specified LONG64 value and sets it to 0.
InterlockedBitTestAndSet Tests the specified bit of the specified LONG value and sets it to 1.
InterlockedBitTestAndSet64 Tests the specified bit of the specified LONG64 value and sets it to 1.
InterlockedCompare64Exchange128 Performs 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.
InterlockedCompare64ExchangeAcquire128 Performs 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.
InterlockedCompare64ExchangeRelease128 Performs 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.
InterlockedCompareExchange 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.
InterlockedCompareExchange64 Performs 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.
InterlockedCompareExchangeAcquire 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. The operation is performed with acquire memory access semantics.
InterlockedCompareExchangeAcquire64 Performs 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.
InterlockedCompareExchangePointer Performs 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.
InterlockedCompareExchangePointerAcquire Performs 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.
InterlockedCompareExchangePointerRelease Performs 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.
InterlockedCompareExchangeRelease 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. The exchange is performed with release memory access semantics.
InterlockedCompareExchangeRelease64 Performs 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.
InterlockedDecrement Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation.
InterlockedDecrement64 Decrements (decreases by one) the value of the specified 64-bit variable as an atomic operation.
InterlockedDecrementAcquire Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedDecrementAcquire64 Decrements (decreases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedDecrementRelease Decrements (decreases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed with release memory access semantics.
InterlockedDecrementRelease64 Decrements (decreases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed with release memory access semantics.
InterlockedExchange Sets a 32-bit variable to the specified value as an atomic operation.
InterlockedExchange64 Sets a 64-bit variable to the specified value as an atomic operation.
InterlockedExchangeAcquire Sets a 32-bit variable to the specified value as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedExchangeAcquire64 Sets a 32-bit variable to the specified value as an atomic operation. The operation is performed with acquire memory access semantics.
InterlockedExchangeAdd Performs an atomic addition of two 32-bit values.
InterlockedExchangeAdd64 Performs an atomic addition of two 64-bit values.
InterlockedExchangeAddAcquire Performs an atomic addition of two 32-bit values. The operation is performed with acquire memory access semantics.
InterlockedExchangeAddAcquire64 Performs an atomic addition of two 64-bit values. The operation is performed with acquire memory access semantics.
InterlockedExchangeAddRelease Performs an atomic addition of two 32-bit values. The operation is performed with release memory access semantics.
InterlockedExchangeAddRelease64 Performs an atomic addition of two 64-bit values. The operation is performed with release memory access semantics.
InterlockedExchangePointer Atomically exchanges a pair of pointer values.
InterlockedExchangePointerAcquire Atomically exchanges a pair of pointer values. The operation is performed with acquire memory access semantics.
InterlockedIncrement Increments (increases by one) the value of the specified 32-bit variable as an atomic operation.
InterlockedIncrement64 Increments (increases by one) the value of the specified 64-bit variable as an atomic operation.
InterlockedIncrementAcquire Increments (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed using acquire memory access semantics.
InterlockedIncrementAcquire64 Increments (increases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed using acquire memory access semantics.
InterlockedIncrementRelease Increments (increases by one) the value of the specified 32-bit variable as an atomic operation. The operation is performed using release memory access semantics.
InterlockedIncrementRelease64 Increments (increases by one) the value of the specified 64-bit variable as an atomic operation. The operation is performed using release memory access semantics.
InterlockedOr Performs an atomic OR operation on the specified LONG values.
InterlockedOrAcquire Performs an atomic OR operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedOrRelease Performs an atomic OR operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedOr8 Performs an atomic OR operation on the specified char values.
InterlockedOr8Acquire Performs an atomic OR operation on the specified char values. The operation is performed with acquire memory access semantics.
InterlockedOr8Release Performs an atomic OR operation on the specified char values. The operation is performed with release memory access semantics.
InterlockedOr16 Performs an atomic OR operation on the specified SHORT values.
InterlockedOr16Acquire Performs an atomic OR operation on the specified SHORT values. The operation is performed with acquire memory access semantics.
InterlockedOr16Release Performs an atomic OR operation on the specified SHORT values. The operation is performed with release memory access semantics.
InterlockedOr64 Performs an atomic OR operation on the specified LONGLONG values.
InterlockedOr64Acquire Performs an atomic OR operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedOr64Release Performs an atomic OR operation on the specified LONGLONG values. The operation is performed with release memory access semantics.
InterlockedXor Performs an atomic XOR operation on the specified LONG values.
InterlockedXorAcquire Performs an atomic XOR operation on the specified LONG values. The operation is performed with acquire memory access semantics.
InterlockedXorRelease Performs an atomic XOR operation on the specified LONG values. The operation is performed with release memory access semantics.
InterlockedXor8 Performs an atomic XOR operation on the specified char values.
InterlockedXor8Acquire Performs an atomic XOR operation on the specified char values. The operation is performed with acquire memory access semantics.
InterlockedXor8Release Performs an atomic XOR operation on the specified char values. The operation is performed with release memory access semantics.
InterlockedXor16 Performs an atomic XOR operation on the specified SHORT values.
InterlockedXor16Acquire Performs an atomic XOR operation on the specified SHORT values. The operation is performed with acquire memory access semantics.
InterlockedXor16Release Performs an atomic XOR operation on the specified SHORT values. The operation is performed with release memory access semantics.
InterlockedXor64 Performs an atomic XOR operation on the specified LONGLONG values.
InterlockedXor64Acquire Performs an atomic XOR operation on the specified LONGLONG values. The operation is performed with acquire memory access semantics.
InterlockedXor64Release Performs an atomic XOR operation on the specified LONGLONG values. The operation is performed with release memory access semantics.

鏈表的互鎖函數:
Singly-linked list function Description
InitializeSListHead Initializes the head of a singly linked list.
InterlockedFlushSList Flushes the entire list of items in a singly linked list.
InterlockedPopEntrySList Removes an item from the front of a singly linked list.
InterlockedPushEntrySList Inserts an item at the front of a singly linked list.
QueryDepthSList Retrieves the number of entries in the specified singly linked list.
RtlFirstEntrySList Retrieves the first entry in a singly linked list.
RtlInitializeSListHead Initializes the head of a singly linked list. Applications should call InitializeSListHead instead.
RtlInterlockedFlushSList Flushes the entire list of items in a singly linked list. Applications should call InterlockedFlushSList instead.
RtlInterlockedPopEntrySList Removes an item from the front of a singly linked list. Applications should call InterlockedPopEntrySList instead.
RtlInterlockedPushEntrySList Inserts an item at the front of a singly linked list. Applications should call InterlockedPushEntrySList instead.
RtlQueryDepthSList Retrieves the number of entries in the specified singly linked list. Applications should call QueryDepthSList instead.

posted on 2007-07-26 22:29 夢在天涯 閱讀(6939) 評論(9)  編輯 收藏 引用 所屬分類: CPlusPlus

評論

# re: C++多線程(四) 2007-07-27 09:30 若弱

第一個例子的結果在默認編譯模式下,用InterlockedAdd和++操作結果是完全一樣的,根本不會出現線程安全的問題  回復  更多評論   

# re: C++多線程(四) 2007-07-27 14:17 dfl

++操作可能是atomic的

不過如果把print語句放到thread里面就能體現出帖子的要表達的意思了  回復  更多評論   

# re: C++多線程(四) 2007-07-27 17:40 夢在天涯

結果一樣嗎,我使用的vs2005,結果是不一樣啊
,最少我敢說有時是不一樣的啊,但可能有寫時候可能正好相同,這個取決與系統不同的線程中斷,這個不由我們控制。

如果看不到結果,你可以把線程數在加大。

++并不是atomic的,是有2條語句的。  回復  更多評論   

# re: C++多線程(四) 2007-09-26 16:37 GINA

reinterpret_cast<long*>(&g_x)
這個參數不太明白。。希望解釋下。謝謝 thank you  回復  更多評論   

# re: C++多線程(四) 2007-09-26 17:06 夢在天涯

reinterpret_cast<long*>(&g_x),是就取地址,然后轉化為指向long的指針。  回復  更多評論   

# re: C++多線程(四) 2008-01-21 21:07 小不點

這不就是原子操作嗎?好象只能用于操作單條語句,要是執行多條語句是不是就不能用了呢?  回復  更多評論   

# re: C++多線程(四) 2008-06-23 10:15 pgmsoul

reinterpret_cast<long*>(&g_x)
寫成(long*)&g_x就完了,說真的,我很BS標準C++的寫法,一看就讓人覺的高深莫測.  回復  更多評論   

# re: C++多線程(四) 2009-06-10 11:12 aniki

這個例子在我的機器上的執行結果有點怪,偶爾會出現小于THREAD_MAX的值,已經使用InterlockedExchangeAdd(reinterpret_cast<long*>(&g_x),1); 了,不只為什么  回復  更多評論   

# re: C++多線程(四) 2010-09-15 17:34 liziyun537

@aniki
我也是這種情況  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1814998
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲精品国产拍免费91在线| 亚洲综合三区| 欧美成人国产va精品日本一级| 亚洲欧洲精品一区二区三区不卡| 亚洲视频在线一区观看| 蘑菇福利视频一区播放| 国产一区二区三区久久悠悠色av| 一区二区三区精品视频| 欧美黄色大片网站| 美女诱惑黄网站一区| 激情久久久久| 久久免费高清| 久久久欧美精品| 伊人春色精品| 欧美a级在线| 久久色在线观看| 亚洲第一区中文99精品| 欧美成人免费小视频| 久久夜色精品国产| 亚洲国产一区二区三区青草影视 | 日韩视频一区| 女仆av观看一区| 久久亚洲春色中文字幕| 欧美精品福利在线| 国产精品免费一区二区三区观看| 中文在线资源观看视频网站免费不卡| 欧美国产先锋| 欧美另类69精品久久久久9999| 亚洲美女色禁图| 一本久道久久综合婷婷鲸鱼| 欧美三级黄美女| 午夜欧美大片免费观看| 欧美在线free| 亚洲人午夜精品免费| 亚洲精品自在久久| 国产精品草草| 久久久免费av| 美国三级日本三级久久99| 99国产精品视频免费观看一公开| 99re在线精品| 国产日韩在线播放| 欧美承认网站| 老司机精品福利视频| 一卡二卡3卡四卡高清精品视频| 亚洲图片欧美午夜| 黄色小说综合网站| 日韩视频中午一区| 黄色免费成人| 在线亚洲激情| 激情一区二区三区| 一本色道久久综合精品竹菊 | 亚洲一区免费网站| 久久精品日产第一区二区| 99精品99久久久久久宅男| 亚洲欧美视频在线观看视频| 亚洲国产精品尤物yw在线观看 | 午夜精品亚洲一区二区三区嫩草| 韩曰欧美视频免费观看| 日韩视频在线免费观看| 极品中文字幕一区| 亚洲无线视频| 日韩视频不卡中文| 欧美在线观看日本一区| 中日韩午夜理伦电影免费| 久久精品一区| 羞羞漫画18久久大片| 欧美激情亚洲综合一区| 久久狠狠婷婷| 国产精品第13页| 亚洲日本电影在线| 亚洲国产精品999| 亚洲一级黄色片| 亚洲欧洲日本在线| 久久国产精品72免费观看| 亚洲一级黄色片| 欧美国产专区| 欧美福利影院| 在线观看亚洲精品| 欧美一区二区日韩| 香港久久久电影| 欧美日韩一区在线播放| 亚洲电影在线播放| 在线观看欧美日本| 欧美在线啊v一区| 欧美一区二区视频在线观看| 亚洲欧美在线网| 亚洲一区二区高清视频| 欧美国产综合视频| 亚洲国产日韩一区二区| 91久久国产自产拍夜夜嗨| 久久另类ts人妖一区二区| 久久精品一区二区三区不卡牛牛| 国产精品精品视频| 一区二区三区高清| 亚洲影院免费观看| 国产精品久久久久一区| 日韩视频在线观看国产| 一本一道久久综合狠狠老精东影业| 欧美二区在线看| 欧美激情成人在线视频| 91久久精品日日躁夜夜躁欧美 | 欧美成人一品| 亚洲国产另类 国产精品国产免费| 亚洲国产老妈| 欧美激情一区二区三区成人| 亚洲狠狠婷婷| 亚洲一区二区三区四区五区午夜| 欧美日韩一区二区三区免费| 夜夜夜久久久| 久久国产精品一区二区三区| 国产真实久久| 美女久久网站| 亚洲精品国产精品国自产在线| 一区二区av| 国产精品一二三四区| 欧美一区二区三区免费大片| 久久一本综合频道| 亚洲国产1区| 欧美人成在线视频| 亚洲午夜av| 久久久久久欧美| 亚洲人成网站在线播| 欧美午夜精品久久久久久超碰| 亚洲一区二区毛片| 久久久久综合一区二区三区| 亚洲国产一区二区a毛片| 欧美日韩精品在线| 欧美在线免费播放| 亚洲高清色综合| 亚洲欧美日韩在线播放| 黄色精品免费| 欧美性做爰猛烈叫床潮| 久久精品夜色噜噜亚洲a∨| 亚洲日韩第九十九页| 久久激情婷婷| 一本色道久久加勒比88综合| 国产午夜精品一区二区三区欧美| 久久美女艺术照精彩视频福利播放| 亚洲精品免费一区二区三区| 欧美一二三视频| 亚洲人被黑人高潮完整版| 国产精品美女黄网| 欧美国产视频一区二区| 欧美在线影院在线视频| 日韩亚洲欧美一区二区三区| 久久亚洲国产成人| 亚洲在线观看免费视频| 91久久精品国产91久久| 国产欧美一区二区精品秋霞影院 | 99热免费精品在线观看| 国产麻豆午夜三级精品| 欧美激情视频在线播放| 久久亚洲图片| 欧美二区在线观看| 亚洲欧美视频在线| 亚洲精品视频在线| 老鸭窝毛片一区二区三区| 亚洲自拍电影| 日韩午夜在线| 亚洲韩日在线| 国产日韩欧美不卡| 欧美先锋影音| 欧美另类人妖| 免费看av成人| 久久亚洲国产精品一区二区 | 久久久精品国产免费观看同学| 一区二区三区波多野结衣在线观看| 免费中文日韩| 美女999久久久精品视频| 久久成人人人人精品欧| 一本色道久久88综合日韩精品| 亚洲高清在线观看一区| 亚洲电影免费观看高清完整版在线| 国产一区二区在线观看免费播放| 国产精品自拍在线| 亚洲一区二区影院| 一区二区三区日韩| 91久久久久久国产精品| 欧美国产综合视频| 免费观看30秒视频久久| 老司机午夜免费精品视频| 久久夜色精品国产噜噜av| 久久免费视频在线观看| 久久综合狠狠综合久久综青草| 久久久久**毛片大全| 久久精品国产一区二区电影| 欧美自拍偷拍午夜视频| 欧美在线一二三区| 一区二区三欧美| 久久精品亚洲乱码伦伦中文| 羞羞漫画18久久大片| 欧美一区二区三区在线| 久久精品九九| 久久久蜜桃一区二区人| 久久综合五月| 欧美国产激情| 亚洲精品美女在线| 中文精品视频一区二区在线观看| 亚洲影音先锋| 久久久久久久97|