Solved

What's Mutex?

Posted on 2001-07-17
2
559 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

623 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