Archived

This topic is now archived and is closed to further replies.

FERNANDO-BRASIL

Multi-Threading and MUTEX

Recommended Posts

Hi! I was wondering how I could use mutexes on a multi-threading application. I'm new on Multithread, tryied to use MUTEX to syncronize the execution of two threads, but all I got were some good headaches. Look how I'm creating two threads and the mutex:
mtx = CreateMutex(NULL, FALSE, "ldskjfjoewrwoiru");
CreateThread(NULL, 0, &threadtest, NULL, 0, &tid);
CreateThread(NULL, 0, &threadtest1, NULL, 0, &tid1);
where mtx, tid and tid1 are declared as HANDLE. Before these code I have threadtest and threadtest1 functions declared as these:
DWORD WINAPI threadteste(LPVOID dfdf) {
   if (WaitForSingleObject(mtx, 100) == WAIT_OBJECT_0) {       
      MessageBox(AppHandle, "Thread 01", "", MB_OK);
      SetEvent(mtx);
   }
}

DWORD WINAPI threadteste1(LPVOID dfdf) {
   if (WaitForSingleObject(mtx, 100) == WAIT_OBJECT_0) {       
      MessageBox(AppHandle, "Thread 02", "", MB_OK);
      SetEvent(mtx);
   }
}
But when I compile and run my application, only one thread is acessed. Where is the problem? Do you know a good documentation about this on the internet (not MSDN, because I have already searched there). Thank you. [edited by - FERNANDO-BRASIL on April 20, 2004 3:11:07 PM]

Share this post


Link to post
Share on other sites
Yes, unless you need to share data among different exes, then mutexes are an overkill. Use CriticalSections instead, they are much faster.

But if you really want to use the mutex, you need to release the ownership, when you have used it, by using ReleaseMutex and not by the SetEvent function you are using now.

Edit: you also need to set the timeout to something bigger than 100, if you are going to release the mutex after you have shown the messagebox, otherwise you have to click ok really fast.

[edited by - fredizzimo on April 20, 2004 3:02:10 PM]

Share this post


Link to post
Share on other sites
Thank you fredizzimo. I thought ReleaseMutex would be used to free the HANDLE pointed by the MUTEX, but now I remembered that to free the MUTEX we have to make a call to CloseHandle function.

By the way, I also thought that calling SetEvent would pass the flow to the other threads. It's because WaitForSingleObject will return when the MUTEX is signaled, and before it returns, it sets the MUTEX to a nonsignaled state. So, I thought signaling the MUTEX would pass the flow to another thread.

Thank you a lot, sorry for the bad english (i'm brazillian), and sorry if my answer were a bit off-topic.

Fernando.


[edited by - FERNANDO-BRASIL on April 20, 2004 3:19:54 PM]

Share this post


Link to post
Share on other sites
Mutexes and Events are two different types of synchronization objects. SetEvent should only apply to Event objects. If you haven''t checked out MSDN''s reference material, here it is. It''ll explain and give examples of how to use each of the various synchronization objects.

Share this post


Link to post
Share on other sites
Sorry if this is slightly unrelated, but I think it''s better never to use named objects if you can avoid it.

Named objects exist in a namespace (in MS Windows) which is either machine-wide or Terminal-Server Session-wide (Depends what type of object and what version of Windows, possibly some other factors), and may prevent multiple instances of your program running correctly.

I don''t think that these synch objects usually need to be named - so don''t name them.

Mark

Share this post


Link to post
Share on other sites
You should be checking the error codes returned from all of those functions. They would have told you that SetEvent is not working.

...
The only reason to use a full-blown mutex is to syncronize access between multiple processes, and in order for them to do that they must all use the same (non-null) name.

If the mutex can be unnamed, you can use a process-local mutex, which is called a CRITICALSECTION in Win32.

Share this post


Link to post
Share on other sites