Solved

What's Mutex?

Posted on 2001-07-17
2
555 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web 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…

751 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