<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 閱讀(666) 評論(2)  編輯  收藏 所屬分類: cppZZ

    評論

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

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

    主站蜘蛛池模板: 免费无码A片一区二三区| 一级毛片a免费播放王色| 182tv免费观看在线视频| 亚洲av无码一区二区三区不卡| 又粗又长又爽又长黄免费视频| 国产精品深夜福利免费观看| 中文字幕亚洲精品无码| 日本特黄特色aa大片免费| 久久综合亚洲色hezyo| 亚洲伊人久久大香线焦| 91手机看片国产永久免费| 国产精品亚洲综合五月天| 久久精品网站免费观看| 亚洲熟妇无码一区二区三区| 国产国产成年年人免费看片| 一级特黄a大片免费| mm1313亚洲国产精品美女| 日日狠狠久久偷偷色综合免费| 国产精品亚洲产品一区二区三区 | 亚洲毛片在线观看| 国产高清免费视频| 亚洲丰满熟女一区二区哦| 国产免费变态视频网址网站| 一级一黄在线观看视频免费| 亚洲成Av人片乱码色午夜| 四虎成年永久免费网站| 久久国产成人精品国产成人亚洲| 毛片基地看看成人免费| 亚洲欧洲日产国产综合网| 最近中文字幕免费mv视频7| 亚洲高清在线视频| 免费大片黄在线观看yw| 理论片在线观看免费| 国产亚洲一区二区精品| 久久亚洲精品11p| 亚洲日韩aⅴ在线视频| 免费下载成人电影| 午夜不卡AV免费| 亚洲第一精品电影网| 在线观看的免费网站无遮挡| 亚洲国产无线乱码在线观看|