Thread: Mutual Exclusion Locks

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Question Mutual Exclusion Locks

    Greetings,
    Can anyone explain what exactly is a mutual exclusion lock and how does one code/use one? Writing a DLL for a multithreaded C program that passes in a void **object which is supposed to point to a MUX. I'm not even sure where to begin.

    Thanks,
    Scott

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I am very familiar with POSIX threads, and can offer you a general definition of a mutex (it appears you are using WinThreads).

    A mutex (mutual exclusion) is a special type of Dijkstra semaphore that allows multiple threads to coordinate access to a shared resource.

    Often in multi-threaded programming, it is desirable for a shared resource to be manipulated by just one thread at a time. The thread will attempt to obtain (lock) the mutex associated with the resource, and will block until it either locks the mutex or, optionally, times out while waiting for the mutex (it is also possible to 'try' a mutex without blocking).

    A thread will be blocked by a mutex if another thread already has that mutex locked. Once the owning thread releases the mutex, any thread waiting on the mutex will automatically attempt to claim it. Once a thread has locked the mutex, it is free to manipulate the data the mutex protects, and must release the mutex when it is done.

    It is possible to write threaded applications without mutexes, but this is rare. Mutexes can become a bottleneck in your code, since they serialize portions of your multi-threaded program.

    Cheers,
    Jason Deckard

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    Hey Jason,
    Thank you for the reply. It makes sense. I remember going over that stuff in college but it's been a while. I'm assuming when you have data to protect in a C program the best way to do it is through the use of a struct? Then I guess you just reference that struct for whichever thread instance you are using.

    Am I even close here?

    thanks,
    Scott

  4. #4
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Yes, in many cases, it makes a lot of sense to group the protected data and mutex in a struct together.
    Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:32 AM
  2. Mutual exclusion with threads
    By axr0284 in forum C++ Programming
    Replies: 10
    Last Post: 12-21-2005, 08:31 AM
  3. HELP! this easy code locks my computer!
    By porsuk in forum C Programming
    Replies: 7
    Last Post: 07-13-2005, 12:47 PM
  4. Xp Locks Up after about 2 hours :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-11-2002, 11:25 AM
  5. Mutual linking
    By Mox in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2001, 06:26 AM