<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 情深深鮮花禮品網
    英文的才原版。  回復  更多評論
      

    主站蜘蛛池模板: 中国国语毛片免费观看视频| 免费精品国产自产拍在| 久久精品国产亚洲av日韩| 1000部夫妻午夜免费| 亚洲夂夂婷婷色拍WW47| 亚洲日韩在线观看| 99精品视频免费观看| 亚洲日韩av无码中文| 亚洲精品国产精品乱码视色| 99视频全部免费精品全部四虎| 噜噜噜亚洲色成人网站| 国产l精品国产亚洲区在线观看| 青青视频观看免费99| 免费一区二区三区在线视频| 337p日本欧洲亚洲大胆色噜噜 | 国产一级在线免费观看| 亚洲影视自拍揄拍愉拍| 亚洲日韩国产精品乱| 日韩av无码成人无码免费 | 最近免费中文字幕4| 插鸡网站在线播放免费观看 | 99国产精品视频免费观看| 日韩欧美亚洲国产精品字幕久久久| 国产亚洲精品一品区99热| 亚洲一区二区久久| 亚洲精品岛国片在线观看| 免免费国产AAAAA片| 午夜影视在线免费观看| 亚洲AV无码专区在线电影成人 | 欧洲黑大粗无码免费| 99免费在线视频| 国产精品亚洲精品日韩动图| 亚洲高清视频免费| 亚洲日韩精品一区二区三区无码| 天天看片天天爽_免费播放| 日韩免费在线视频| 国产99精品一区二区三区免费| 亚洲欧美一区二区三区日产| 亚洲自偷自拍另类图片二区| 99精品免费视品| 美女黄色毛片免费看|