Thread: Mutex in C

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    6

    Mutex in C

    Hello all,
    I'm having a problem locking a resource in a DLL. There are concurrent threads accessing my library, and I need to lock and set a global variable the moment the first thread accesses a certain function. The problem is that evey method I have tried fails to set the mutex lock before other threads have read the mutex condition.

    One method I tried was to simply use a global, boolen flag, initalize it to 0, and check to see if it is 0 upon entry to the function. So, in theory, if thread_1 hits the function, it would find the flag set to false, set it to true, do what it needs to do, and set it back to false. But what actually happend is that thread_1 finds that the flag is false, and before it sets it to true, thread_2 through thread_15 also find that it is false. The result is that 15 threads are accessing the same resource simutaneously.

    I have also tried starting the function off with a while loop with the condition that while the flag is true loop. The next statement sets the flag to true if it is false. Again, in theory, the loop condition should be skipped by thread_1, and the global flag should then be set to true, thus causing all subsequent threads to wait in the loop. But the problem here is the same: by the time thread_1 sets the flag to true, the threads on its heels have passed the while loop.

    I'm sure that I need to use lower level C calls than the ones I have been using, but I can't seem to find any information that is OS independant. (For example, I have read about pthread on Solaris, but I'm currently on Windows, and I will also need to port to AIX). Any ideas?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    In windows you use CreateMutex function and WaitForSingleObject to try the locks...
    http://msdn.microsoft.com/library/de...reatemutex.asp
    http://msdn.microsoft.com/library/de...ngleobject.asp


    In posix you use this
    http://www.xgc.com/manuals/xgclib/x5257.html

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but I can't seem to find any information that is OS independant.
    That's because there isn't anything.
    Standard C doesn't know about processes or threads.

    > Any ideas?
    Create a file called porting.c which contains functions you desire, and which are easy to port to your anticipated operating systems.
    At least then, for each function you need, you only need to edit one file in one place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Read and digest this you may find it helpful.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, it won't be the first time Schmidt has been busted:
    http://www.cs.umd.edu/~pugh/java/mem...edLocking.html

    Even though that's primarily in Java, it's broken in C/C++ for the same reasons.

    DCL can be implemented correctly in Win32, but you really need to know what you're doing:
    http://www.microsoft.com/whdc/driver...MP_issues.mspx

    jgs - I highly recomend that you simply protect access to your resource with a critical section.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. mutex problem
    By radeberger in forum C++ Programming
    Replies: 16
    Last Post: 03-20-2009, 04:24 PM
  3. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  4. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  5. Mutex - location of lock/unlock
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2003, 12:28 PM