Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1006257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:25:45+00:00 2026-05-16T08:25:45+00:00

I read a few documents about Mutex and still the only Idea I have

  • 0

I read a few documents about Mutex and still the only Idea I have got is that it helps preventing threads from accessing a resource that is already being used by another resource.

I got from Code snippet and executed which works fine:

#include <windows.h>
#include <process.h>
#include <iostream>
using namespace std;


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;
}

void main()
{
    HANDLE hMutex;

    hMutex=CreateMutex(NULL,FALSE,"MutexExample");

    if (hMutex == NULL)
    {
        printf("CreateMutex error: %d\n", GetLastError() );
    }
    else if ( GetLastError() == ERROR_ALREADY_EXISTS )
        printf("CreateMutex opened existing mutex\n");

    else
        printf("CreateMutex created new mutex\n");

}

But What I don’t understand is where is the thread and where is the shared resource? Can anyone please explain or provide a better article or document?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:25:46+00:00Added an answer on May 16, 2026 at 8:25 am

    A mutex provides mutually exclusive access to a resource; in your case, a database. There aren’t multiple threads in your program, but you can have multiple instances of your program running, which is what your mutex is protecting against. Effectively, it is still protecting against access from more than one thread, it’s just that those threads can be in separate processes.

    Your code is creating a named mutex that can be shared across multiple instances of your application. This is a form of interprocess communication. MSDN documentation on CreateMutex has additional helpful information about named mutexes:

    Two or more processes can call
    CreateMutex to create the same named
    mutex. The first process actually
    creates the mutex, and subsequent
    processes with sufficient access
    rights simply open a handle to the
    existing mutex…

    Multiple processes can have handles of
    the same mutex object, enabling use of
    the object for interprocess
    synchronization.

    A mutex is only necessary here if the database you’re working against doesn’t inherently support multithreaded access.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions about git. I have read a couple of documents
I've read a few threads on SO about usefulness of unit-testing various applications. The
A few months ago I read about a technique so that if there parameters
I've read a few instances in reading mathematics and computer science that use the
I've read a few posts where people have stated (not suggested, not discussed, not
Sharepoint isn't the speediest of server applications, and I've read about a few tips
I need to read smallish (few MB at the most, UTF-8 encoded) XML files,
As probably many people around here I read a few webcomics. Drowtales is my
I'm looking for the least impacting method to read a few different log files.
I'm just starting to play around with directx and I've read a few articles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.