Solved

What's Mutex?

Posted on 2001-07-17
2
540 Views
Last Modified: 2010-05-18
I got a program (MSVC) from someone else. There is a function called CreateMutex() in it which generate a mutex object. What's call Mutex? Why we use this function?
0
Comment
Question by:chenwei
2 Comments
 
LVL 3

Accepted Solution

by:
rondi earned 20 total points
Comment Utility
A Mutex is a special area of memory managed by the
Operating System (Windows).

It is used for inter-process synchronization.

If two processes (programs) need to access and modify
shared memory (eg. global variables), it is important that one waits for the other, or else the memory may be corrupt/invalid.

The first process will call CreateMutex() to initialize the
Mutex Object (not the memory per se) with a given name.

The second program will call either OpenMutex() or CreateMutex() to get a handle to the Mutex Object with the
same name.

When one of the processes needs to access the shared memory, it calls WaitForSingleObject() with the mutex handle and the max waiting period as parameters.

When it's finished with the memory, it calls
ReleaseMutex(). At this point, the next waiting process can
get ownership of the mutex.

Example use of a Mutex:
-----------------------
Since mutexes are system-wide, a program can use a mutex
to make sure that it only runs 1 instance at a time.
When the program starts it tries to CreateMutex() using
it's name (eg. 'progx_exe'). If it succeeds, then it runs
as usual.
If it fails then it assumes there's another copy
of the program already running and exits before any damage
is done (without trying to delete the mutex). The first program will delete it's mutex when it exits.
0
 

Author Comment

by:chenwei
Comment Utility
Thank you.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now