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

    評論

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

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

    主站蜘蛛池模板: 成人最新午夜免费视频| 国产好大好硬好爽免费不卡 | 亚洲va成无码人在线观看| 国产情侣久久久久aⅴ免费| 国产美女亚洲精品久久久综合| www免费插插视频| 久久久久无码专区亚洲av| 三上悠亚在线观看免费| 亚洲精品午夜无码电影网| 国产精品网站在线观看免费传媒 | 亚洲av高清在线观看一区二区 | 亚洲日本一线产区和二线 | 在线看片人成视频免费无遮挡| 中文字幕亚洲综合小综合在线| 免费无码精品黄AV电影| 亚洲熟妇久久精品| 国产yw855.c免费视频| 一二三区免费视频| 亚洲AV无码一区二区乱孑伦AS| 91成人免费观看| 亚洲欧美日韩综合俺去了| 亚洲AV无码一区二区三区国产| 最近更新免费中文字幕大全| 亚洲宅男永久在线| 日韩免费观看视频| 国产免费内射又粗又爽密桃视频| 亚洲av无码乱码国产精品| 在人线av无码免费高潮喷水| 无套内射无矿码免费看黄| 亚洲视频在线播放| 性做久久久久免费看| a在线视频免费观看在线视频三区| 亚洲高清在线mv| 国产免费黄色大片| 久久久久久久99精品免费观看| 亚洲偷自精品三十六区| 久久影院亚洲一区| 青青草a免费线观a| 久久久久久毛片免费看| 亚洲国产综合自在线另类| 亚洲精品无码久久久久AV麻豆|