<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

    Using Mutex Objects

    You can use a mutex object to protect a shared resource from simultaneous access by multiple threads or processes. Each thread must wait for ownership of the mutex before it can execute the code that accesses the shared resource. For example, if several threads share access to a database, the threads can use a mutex object to permit only one thread at a time to write to the database.

    In the following example, a process uses the CreateMutex function to create a named mutex object or open a handle to an existing mutex object.

    HANDLE hMutex;
    // Create a mutex with no initial owner.
    hMutex = CreateMutex(
    NULL,                       // no security attributes
    FALSE,                      // initially not owned
    "MutexToProtectDatabase");  // name of mutex
    if (hMutex == NULL)
    {
    // Check for error.
    }

    When a thread of this process writes to the database, as in the next example, it first requests ownership of the mutex. If it gets ownership, the thread writes to the database and then releases its ownership.

    The example uses structured exception-handling syntax to ensure that the thread properly releases the mutex object. The __finally block of code is executed no matter how the __try block terminates (unless the __try block includes a call to the TerminateThread function). This prevents the mutex object from being abandoned inadvertently.

    BOOL FunctionToWriteToDatabase(HANDLE hMutex)
    {
    DWORD dwWaitResult;
    // Request ownership of mutex.
    dwWaitResult = WaitForSingleObject(
    hMutex,   // handle to mutex
    5000L);   // five-second time-out interval
    switch (dwWaitResult)
    {
    // The thread got mutex ownership.
    case WAIT_OBJECT_0:
    __try {
    // Write to the database.
    }
    __finally {
    // Release ownership of the mutex object.
    if (! ReleaseMutex(hMutex)) {
    // Deal with error.
    }
    break;
    }
    // Cannot get mutex ownership due to time-out.
    case WAIT_TIMEOUT:
    return FALSE;
    // Got ownership of the abandoned mutex object.
    case WAIT_ABANDONED:
    return FALSE;
    }
    return TRUE;
    }
    posted on 2007-08-17 16:13 jinfeng_wang 閱讀(661) 評論(2)  編輯  收藏 所屬分類: cppZZ

    評論

    # re: Using Mutex Objects (zz) 2007-11-15 18:24 ZHANG/WEI
    都是英文,有些看不清楚哦
    能不能換成中文啊,這樣好像好點,對嗎?  回復  更多評論
      

    # re: Using Mutex Objects (zz) 2008-01-19 16:45 情深深鮮花禮品網
    英文的才原版。  回復  更多評論
      

    主站蜘蛛池模板: 成人午夜免费福利视频| 成人毛片18女人毛片免费| 亚洲自偷精品视频自拍| 免费国产成人高清在线观看网站| 亚洲综合无码一区二区痴汉| 亚洲精品视频在线看| 99久久免费中文字幕精品| 亚洲爆乳AAA无码专区| 亚洲精品制服丝袜四区| 国产无人区码卡二卡三卡免费| 黄网站色成年片大免费高清| 亚洲av综合色区| 日本久久久免费高清| 无码AV片在线观看免费| 亚洲av纯肉无码精品动漫| 国产av天堂亚洲国产av天堂 | a毛片免费在线观看| 国产色在线|亚洲| 亚洲精品少妇30p| 国产精品成人四虎免费视频| 久久香蕉国产线看免费| 国产AV无码专区亚洲AV琪琪| 亚洲精品国产手机| 亚洲精品无码成人片在线观看| 精品无码免费专区毛片| 亚洲一级片免费看| 亚洲一卡2卡三卡4卡无卡下载| 亚洲国产精品无码久久久蜜芽| 日本免费福利视频| 日日麻批免费40分钟日本的| 中文在线观看国语高清免费| 鲁死你资源站亚洲av| 亚洲国语在线视频手机在线| 亚洲乱码国产一区三区| 亚洲国产精品无码久久青草| 最近中文字幕无免费视频| 91成人免费观看| 中文字幕一区二区免费| 美女被免费网站在线视频免费| 亚洲激情视频图片| 久久亚洲精品国产精品|