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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {C#基礎(chǔ)}

C++多線程(四)

多線程同步之WIN API互鎖函數(shù) (可以避免使用CriticalSection或Mutex

一 互鎖函數(shù)

      互鎖函數(shù)的家族十分的龐大,可以查看msdn(http://msdn2.microsoft.com/en-us/library/ms686360.aspx)以InterLocked開始的函數(shù)都是戶數(shù)函數(shù)。使用互鎖函數(shù)的優(yōu)點是:他的速度要比其他的CriticalSection,Mutex,Event,Semaphore快很多。

二 簡單實例

      使用一些實例說明部分互鎖函數(shù)的使用:

1) LONG InterlockedExchangeAdd(   PLONG plAddend,LONG Increment);

簡單實例,在線程函數(shù)中對全局的變量自增,在開始使其為0,在線程都執(zhí)行完以后輸出全局變量的值,如果我們不使用互鎖函數(shù),則最后輸出的結(jié)果,大部分情況是不正確的,比如我們一共有10000個線程的話,則全局變量的值一般是比10000要小;但是如果我們使用互鎖函數(shù)來實現(xiàn)自增,則就快速的實現(xiàn)了線程安全,最后輸出的全局變量一定是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); 來實現(xiàn)g_x的線程安全的自增。  

2)使用LONG InterlockedExchange(PLONG plTarget,LONG lValue);實現(xiàn)循環(huán)鎖:

// 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)太多了,不舉了,以后用到了再放到這里把。

三 互鎖函數(shù)列表

一般的互鎖函數(shù):
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.

鏈表的互鎖函數(shù):
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 夢在天涯 閱讀(6946) 評論(9)  編輯 收藏 引用 所屬分類: CPlusPlus

評論

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

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

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

++操作可能是atomic的

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

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

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

如果看不到結(jié)果,你可以把線程數(shù)在加大。

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

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

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

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

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

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

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

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

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

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

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

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

@aniki
我也是這種情況  回復(fù)  更多評論   

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1819137
  • 排名 - 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>
              亚洲国产第一页| 久久精品国产99精品国产亚洲性色| 日韩一区二区精品视频| 欧美剧在线观看| 亚洲视频在线播放| 久久三级福利| 亚洲伦理自拍| 国产精品久久久久影院亚瑟| 欧美一区亚洲二区| 亚洲成人在线视频播放 | 欧美精品麻豆| 亚洲一区一卡| 欧美wwwwww| 亚洲在线观看视频网站| 国产亚洲精品久久久| 欧美成人一区二免费视频软件| 日韩一级在线观看| 久久久久天天天天| 一区二区三区精品在线| 国产欧美欧洲在线观看| 嫩草影视亚洲| 亚洲欧美日韩一区二区三区在线| 欧美freesex8一10精品| 亚洲夜间福利| 在线欧美日韩| 国产精品日韩一区| 欧美大色视频| 欧美在线视频在线播放完整版免费观看 | 欧美成人精品在线视频| 亚洲——在线| 亚洲国产色一区| 久久精品国产欧美激情| 在线视频欧美日韩| 在线观看欧美精品| 国产精品五区| 欧美高清在线视频观看不卡| 亚洲欧美日韩系列| 日韩视频―中文字幕| 蜜臀av在线播放一区二区三区| 亚洲午夜国产成人av电影男同| 一色屋精品视频免费看| 国产精品色午夜在线观看| 欧美国产综合| 久久综合电影| 久久久www成人免费精品| 亚洲性xxxx| 日韩午夜激情电影| 欧美激情精品久久久久| 久久―日本道色综合久久| 亚洲欧美韩国| 一区二区高清视频| 亚洲精品乱码久久久久久| 伊人久久婷婷色综合98网| 国产婷婷色一区二区三区在线| 欧美亚洲第一页| 欧美日韩亚洲高清一区二区| 欧美国产日韩一区二区三区| 久久蜜桃资源一区二区老牛| 欧美中文在线字幕| 欧美亚洲免费在线| 亚洲欧美另类综合偷拍| 亚洲一级特黄| 亚洲综合大片69999| 在线亚洲免费| 亚洲视频视频在线| 亚洲图片在线观看| 亚洲尤物在线| 午夜在线一区| 久久精品成人一区二区三区| 欧美精品在线网站| 久久影视三级福利片| 久久久亚洲精品一区二区三区| 久久精品国产亚洲精品| 久久国产主播| 噜噜噜91成人网| 免费av成人在线| 欧美国产一区二区三区激情无套| 欧美成人在线影院| 欧美喷水视频| 国产精品成人一区二区| 国产精品热久久久久夜色精品三区 | 亚洲国产高清一区| 最新日韩在线视频| 日韩亚洲一区在线播放| 一区二区免费在线观看| 亚洲欧美电影在线观看| 欧美诱惑福利视频| 久久在线免费视频| 欧美肥婆在线| 国产精品v欧美精品∨日韩| 国产精品视屏| 在线欧美电影| 夜夜嗨av一区二区三区四季av | 亚洲一区二区三区三| 欧美一区二粉嫩精品国产一线天| 久久精品视频在线| 欧美二区乱c少妇| 亚洲精品一区二区三区婷婷月| 一区二区三区.www| 欧美中文字幕在线播放| 欧美成人性生活| 国产精品久久久久av| 国内自拍一区| 99视频国产精品免费观看| 欧美一区二区成人| 亚洲成人资源网| 亚洲一区二区三区精品在线| 久久久久久久久久久久久女国产乱| 欧美国产高清| 国产婷婷色一区二区三区| 亚洲精品一区二区在线| 午夜一级在线看亚洲| 欧美国产精品v| 亚洲影院污污.| 欧美暴力喷水在线| 国产精品黄页免费高清在线观看| 樱桃视频在线观看一区| 亚洲自拍偷拍色片视频| 免费欧美在线| 亚洲男人av电影| 欧美激情1区2区| 韩国一区电影| 亚洲欧美资源在线| 亚洲国产欧美久久| 久久精品官网| 国产精品美女主播| 日韩视频亚洲视频| 久久综合激情| 亚洲欧美在线高清| 欧美日韩午夜| 亚洲啪啪91| 久久婷婷久久| 午夜精品福利电影| 国产精品免费看片| 一区二区三区国产精品| 欧美二区在线播放| 久久久欧美精品| 国产视频精品xxxx| 香蕉成人伊视频在线观看| 亚洲精品国产欧美| 免费国产自线拍一欧美视频| 韩国精品在线观看| 久久精品国产欧美激情| 亚洲伊人观看| 国产精品久久久久久久久久久久久 | 国产精品久久久久久久久久三级| 亚洲欧洲日产国码二区| 美日韩精品视频| 久久国产99| 国产一区视频观看| 欧美在线日韩精品| 亚洲永久免费精品| 国产精品一区二区黑丝| 午夜精品福利视频| 亚洲天堂网在线观看| 国产精品美女久久久免费| 亚洲午夜激情在线| 一本色道久久综合精品竹菊 | 亚洲福利专区| 欧美激情一二三区| 欧美成人免费小视频| 亚洲欧洲精品一区二区三区不卡| 欧美**字幕| 欧美r片在线| 亚洲美女色禁图| 亚洲精品综合在线| 欧美三区在线视频| 香蕉久久精品日日躁夜夜躁| 亚洲欧美日韩人成在线播放| 国产欧美精品一区二区色综合 | 欧美粗暴jizz性欧美20| 欧美jizz19性欧美| 一本色道久久综合亚洲精品按摩| 亚洲久久一区二区| 欧美体内she精视频| 欧美一区二区三区在线看| 亚洲欧美在线网| 激情综合久久| 亚洲国产成人不卡| 国产精品v欧美精品v日韩 | 在线综合视频| 国产精品一区二区男女羞羞无遮挡| 性欧美18~19sex高清播放| 久久成人18免费观看| 亚洲高清免费| 99视频在线观看一区三区| 国产精品午夜在线| 美女视频黄免费的久久| 欧美精品在线极品| 欧美一区二区高清在线观看| 久久久久久91香蕉国产| 亚洲九九九在线观看| 亚洲欧美成人网| 亚洲国产专区| 亚洲一区二区三区精品动漫| 亚洲成人在线免费| 这里只有精品视频在线| 伊甸园精品99久久久久久| 亚洲精选91| 尤物精品在线|