Solved

What is the difference between CMutex and CEvent?

Posted on 2008-10-24
8
778 Views
Last Modified: 2013-11-20
Hi,
What is the difference between CMutex and CEvent?
It looks to me that they do the same work.

Thanks
0
Comment
Question by:UdiRaz
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 22801726
Both are used in a multi-threaded environment to insure two threads do not attempt to access the same variable at the same time.

A CMutex works by blocking a thread's access to a shared resource while any other thread has access to the shared resource.  You can think of it like this.  A shared resource has one-and-only-one "flag".  A CMutex requests ownership of the "flag", and waits until it's given the "flag".  So basically a thread is attempting to do a job, but a CMutex blocks continued processing until the resource becomes available.

A CEvent works more like an event driven system.  A thread is set up to do some work.  But the thread sits in a wait state doing nothing until another thread, through the CEvent, activates the thread waiting on the CEvent.  So basically, a thread is attempting to do nothing until a CEvent says it's time to do a block of work.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22802772
To add to above explanation:

Look at the member functions of CMutex and CEvent.

The CMutex has only Lock and Unlock. So you can write code like


void MyClass::addToMessageQueue(MyMsg msg)
{
       myMutex.Lock();
       myqueue.push_back(msg);
       myMutex.Unlock();
}

which makes the update of a queue thread-safe in case any access function to the queue has the same lock and unlock. If any of these functions protected by Lock and Unlock was called by a second thread, this second thread waits (automatically) when calling Lock() until the first thread has left the exclusive sequence by calling Unlock.


The CEvent has SetEvent, PulsEvent, ResetEvent and Unlock, which allow a more differentiated usage. You can set an event to signaled (what would release any thread wait for the signal) or reset the signal what would cause any other thread which want get access to the event to wait until it was signaled. The threads would use the CEvent not directly but by means of a CSingleLock or CMultiLock, so for them CMutex has indeed a similar functionality. But from the thread that created the CEvent it is much different.
0
 

Author Comment

by:UdiRaz
ID: 22806313
Do Mutex and evrnts work with threads from different process or all thread must be created within the same process?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22806541
>>>>> all thread must be created within the same process?
a thread is *defined* as one independent subcomponent of one process. So each process has at least one thread (the main thread) and must create its own threads. You can't create a thread and attach it to a different process (beside of writing a new operation system which would allow this).
0
 

Author Comment

by:UdiRaz
ID: 22806559
What I ment is can Mutex and/or event synchronize between events from two defference process?

If I will create an even or a mutex with the "Text" in two difference processed?
Will one wait until a second will be reset/unlock?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22806815
>>>> If I will create an even or a mutex with the "Text" in two difference processed?
Yes, mutex and events can synchronize between processes cause they are resources managed of the operation system.

There is a 'fast mutex' called CRITICAL_SECTION in Windows. That can be used only within one process and its threads.
0
 

Author Comment

by:UdiRaz
ID: 22806825
I DO want to use a synch boject between two different processes !!!

Can I use mutex?
Can I use event?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 22807215
>>>> Can I use mutex?

Yes. You need a named mutex with same name for both processes.

Use the following constructor:

CMutex( BOOL bInitiallyOwn = FALSE, LPCTSTR lpszName = NULL, LPSECURITY_ATTRIBUTES lpsaAttribute = NULL );

for both processes. The bInitiallyOwn tells whether you already want to lock the mutex initially. I would recommend against it.

Note, Windows will use the same internal mutex resource in case a second process use the same name. Hence, you can use it beyond process boundaries.

>>>> Can I use event?

Here the same applies. If used without name it only could be used between threads of one process. If used with name it can be used between processes (and their threads).



0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 85
wait notify demo infinite loop 3 110
trigs fail! I thought I knew how to do trignometry 3 67
c++, dynamic object by json 1 42
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

803 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