How do I perform a mutex lock in a C win32 crt application?

I create several threads in my application with CreateThread.   I would like to serialize access to some data structures but I don't know how to create and use a mutex in windows.   Could anyone provide me with some example code?
threads[i] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DatabaseUpdate, &context, 0, &threadId );	
	threads[i+1] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ClientWorker, &context, 0, &threadId );
	threads[i+2] = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)BackgroundScanner, &context, 0, &threadId);

Open in new window

DrachsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DanRollinsCommented:
There's a good discussion here:
    Multithreading for Rookies
    http://msdn.microsoft.com/en-us/library/ms810438.aspx
You will use
    CreateMutex
    http://msdn.microsoft.com/en-us/library/ms682411.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Some little helper thatI found useful using over the time (with some self-explanatory sample code):
HANDLE ConnectEvent ( LPCTSTR pszName)
{
 
   HANDLE hEvent   =   CreateEvent (   NULL,
                                       FALSE,
                                       FALSE,
                                       pszName
                                   );
 
   if  (   INVALID_HANDLE_VALUE    ==  hEvent)
       {
           if  (   ERROR_ALREADY_EXISTS    ==  GetLastError    ())
               {
                   hEvent  =   OpenEvent   (   SYNCHRONIZE,
                                               FALSE,
                                               pszName
                                           );
 
                   if  (   INVALID_HANDLE_VALUE    ==  hEvent)
                       {
 
                           return NULL;
                       }
               }
       }
 
   return hEvent;
}
 
HANDLE ConnectMutex ( LPCTSTR pszName)
{
 
   HANDLE hMutex   =   CreateMutex (   NULL,
                                       FALSE,
                                       FALSE,
                                       pszName
                                   );
 
   if  (   INVALID_HANDLE_VALUE    ==  hMutex)
       {
           if  (   ERROR_ALREADY_EXISTS    ==  GetLastError    ())
               {
                   hMutex  =   OpenMutex   (   SYNCHRONIZE,
                                               FALSE,
                                               pszName
                                           );
 
                   if  (   INVALID_HANDLE_VALUE    ==  hMutex)
                       {
 
                           return NULL;
                       }
               }
       }
 
   return hMutex;
}
 
HANDLE hEvent;
HANDLE hMutex;
char acSharedData [255];
 
LONG WINAPI ThreadOne (LPVOID)
{
 
    for ( ;;)
    {
        Sleep(rand());
 
        WaitForSingleObject(hMutex,INFINITE);
 
        strcpy(acSharedData,"Set by ThreadOne");
 
        ReleaseMutex(hMutex);
 
        SetEvent(hEvent);
    }
 
}
 
LONG WINAPI ThreadTwo (LPVOID)
{
 
    for ( ;;)
    {
 
        DWORD dwRes = WaitForSingleObject(hEvent,1);
 
        if (WAIT_WIMEOUT != dwRes) // event has been signalled
        {
            WaitForSingleObject(hMutex,INFINITE);
 
            strcpy(acSharedData,"Set by ThreadTwo");
 
            ReleaseMutex(hMutex);
        }
 
    }
 
}
 
 
 
int _tmain(int argc, _TCHAR* argv[])
{
     // initialisation des options pour la fonction CreateThread
     HANDLE hThread;
     DWORD dwThread;
     HANDLE hThread2;
     DWORD dwThread2;
 
     hEvent = ConnectEvent(NULL);
     hMutex = ConnectMutex(NULL);
 
     hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) ThreadOne, (LPVOID)NULL, NULL, &dwThread);
     hThread2 = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE) ThreadTwo, (LPVOID)NULL, NULL, &dwThread2);
     
    for ( ;;)
    {
        Sleep(rand());
 
        WaitForSingleObject(hMutex,INFINITE);
 
        WaitForSingleObject(hMutex,INFINITE);
 
        printf("%s\n",acSharedData);
 
        ReleaseMutex(hMutex);
    }
 
}

Open in new window

0
DrachsAuthor Commented:
Thanks, sorry I had to split your points but both answers were really good quality.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.