Thread: semaphore or mutex

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    semaphore or mutex

    I read somewhere:
    "With a mutex, only one thread can own it at any given moment. With a semaphore, multiple threads can own it at a time."

    I don't understan why to allow multiple threads own a semaphore at the same time. Isn't that a new potential synchronization problem?

    If someone explain me in which situations to use mutex and in which situations to use semaphores?

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I had an application which ran on some very different computers. Some had masses of memory and fast CPU's others were "challenged" in the capability arena.

    The application processed files received from a wireless LAN. The processing of each file was atomic, i.e. it idn't use shared data.

    By experiment, I discovered that there was an optimum number of threads per machine. Machine A would get through a 1000 files in the shortest time with 150 threads running. Machine B had the best performance with 60 threads running. In the end, I developed an algorithm that "tuned" the number of threads running as the process ran. The way I enforced the thread count limit was to create threads and have them immediately wait for a semaphore which had the lMaximumCount value set to the calculated value. If there were less than that number of threads running, the new thread ran at once, if there were already enough threads running, the threads queued themselves waiting for other threads to finish.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I'm really interested in knowing how you had it 'tune' the lMaximumCount value. Care to explain?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-11-2007, 04:59 PM
  2. Semaphore Question
    By azamsharp1 in forum C Programming
    Replies: 4
    Last Post: 10-30-2005, 09:01 AM
  3. CreateSemaphore/ReleaseSemaphore
    By nrieger in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2005, 06:57 AM
  4. Producer-comsumer threads using semaphore mutex
    By Kaiya in forum C Programming
    Replies: 3
    Last Post: 04-29-2004, 08:17 AM
  5. Mutex vs. Semaphore
    By cjschw in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2003, 07:03 PM