Link to home
Start Free TrialLog in
Avatar of UdiRaz
UdiRaz

asked on

What is the difference between CMutex and CEvent?

Hi,
What is the difference between CMutex and CEvent?
It looks to me that they do the same work.

Thanks
Avatar of HooKooDooKu
HooKooDooKu

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.
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.
Avatar of UdiRaz

ASKER

Do Mutex and evrnts work with threads from different process or all thread must be created within the same process?
>>>>> 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).
Avatar of UdiRaz

ASKER

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?
>>>> 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.
Avatar of UdiRaz

ASKER

I DO want to use a synch boject between two different processes !!!

Can I use mutex?
Can I use event?
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial