Solved

What's Mutex?

Posted on 2001-07-17
2
545 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
ID: 6289160
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
ID: 6290992
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

20 Experts available now in Live!

Get 1:1 Help Now