Mutex in C

This is a discussion on Mutex in C within the Windows Programming forums, part of the Platform Specific Boards category; Hello all, I'm having a problem locking a resource in a DLL. There are concurrent threads accessing my library, and ...

  1. #1
    jgs
    jgs is offline
    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
    #include<xErath.h> xErath's Avatar
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,674
    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, 04:34 AM
  5. Mutex - location of lock/unlock
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 08-14-2003, 12:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21