Click here to Skip to main content
Click here to Skip to main content

Tagged as

Creating and Using Mutex Objects

, 19 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This article shows the use of mutex's with explanations (Copied from MSDN - may prove convenient for some)
I know, it's already there in MSDN, but it's hard to find. So I am pasting it here to refer to some of my colleagues for better understanding of mutex
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 mutex object.
HANDLE hMutex; 
// Create a mutex with no initial owner.

hMutex = CreateMutex( 
    NULL,                       // default security attributes
    FALSE,                      // initially not owned
    NULL);                      // unnamed mutex

if (hMutex == NULL) 
    printf("CreateMutex error: %d\n", GetLastError());
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.
        // 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; 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mukit, Ataul
Chief Technology Officer Rational Technologies
Bangladesh Bangladesh
Your Lord does not like people who waste by extravagance, and only the righteous C s, and does so in increments, and they are of those people who are mindful of what memory and time has in offer and they waste none of it.
Follow on   Twitter

Comments and Discussions

Questiongood PinmemberCIDev25-Jun-12 5:36 
QuestionNice example PinmemberJackDingler14-Mar-12 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 19 Feb 2012
Article Copyright 2012 by Mukit, Ataul
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid