Semaphore question

This is a discussion on Semaphore question within the C Programming forums, part of the General Programming Boards category; If I have multiple threads all waiting on the same semaphore (the semaphores being in shared memory), is there any ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Semaphore question

    If I have multiple threads all waiting on the same semaphore (the semaphores being in shared memory), is there any rule as to which gets allowed to run first when that semaphore is queued?

    My guess to this answer would be no; that the thread which gets to pass on the one queue signal would be whichever thread is "lucky" enough to get it's CPU time first after the semaphore is signaled/queued.

    If this is the case, is there any way to specify that you'd like the order of precedence to be the order in which they arrived?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Well, from a Win32 perspective, calling ReleaseSemaphore with a release count of the total number threads would release all the threads at the same time. Thus, they would all run "first". I really don't know if this is a good idea. There are contention and race issues that may have to be dealt with.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Mostly I would say it's a FIFO arrangement, the first thread to wait on the semaphore will be the next one to run when the semaphore is released.

    Other systems use priority as well (if your threads have a priority), then the thread at the head of the highest priority queue waiting on the semaphore will be released.

    If you want more specifics, state your OS/Compiler/Thread library

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    An answer from a Win32 perspective:

    On the lowest level, the scheduler places these threads in a FIFO (First In/First Out) queue. On the application level, the order in which the threads are released is not always FIFO. The reason for this is that a waiting thread may be scheduled to perform an APC (asynchronous procedure call) such as an I/O completion routine. When the thread returns from the APC, it will once again be placed at the tail end of the queue of threads waiting for the synchronization object. If you have a situation in which it is critical for threads to be released in the order in which they arrived, you must implement your own queue on the application level. This will incur overhead as well as programming complexity and should really be avoided if possible

    references: Advanced Windows, Richter, Microsoft Press and Multithreading Applications in Win32, Berveridge & Weinger, Addison Wesley

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. semaphore
    By menzeli in forum Linux Programming
    Replies: 12
    Last Post: 04-15-2007, 09:26 AM
  2. semaphore
    By menzeli in forum C Programming
    Replies: 1
    Last Post: 03-24-2007, 11:34 AM
  3. clueless about the semaphore object
    By y_cant_i_C in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2006, 01:03 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

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