Skip to main content
gamedev.net
🔒 Locked

Mutex Question

Started by Ryanrk 0 replies 450+ views
Original Post
Ryanrk
Ryanrk
Ok, i think i'm having some issues with threads messing up the main program by calling the same function twice at the same time so i'm trying to use the mutex and was wondering if i'm doing this right. this is just a sample that i created that kind of represents my program. Now is this created? because this is what i have and it seems to not bw working. As you can see I don't want Variable A below being access by the one function when the other is using it.
    
//this is global

HANDLE mutex = CreateMutex(NULL, FALSE, NULL);

int main(){

    while(1){

        while(WaitForSingleObject(mutex, 0) != WAIT_OBJECT_0);
	// call function A that uses variable A

        ReleaseMutex(mutex);
    }
    return 1;
}
DWORD WINAPI thethread(LPVOID threadNo){
   while(1){
	
        while(WaitForSingleObject(mutex, 0) != WAIT_OBJECT_0);
	    //call function B that uses variable A 

        ReleaseMutex(mutex);
   }
}
    
[edited by - Ryanrk on July 31, 2002 6:33:16 PM]
smanches
smanches
That is the right way to do it. One good practice to prevent deadlock is to lock all variables accessed by both threads with one mutex.

You might want to look into CriticalSections as well since they are much lower overhead than a mutex for these things. I guess it doesn''t really matter, I''m just a freak about optimizations. :-)

Stephen Manchester
Senior Technical Lead
Virtual Media Vision, Inc.
stephen@virtualmediavision.com
(310) 930-7349
Stephen ManchesterSenior Technical LeadVirtual Media Vision, Inc.stephen@virtualmediavision.com(310) 930-7349

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.