Thread: semaphores help

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    semaphores help

    i created 11 threads..... and each thread is doing a function the 11th thread should execute after all the 10 thread processing is over iam using semaphore for this i need help... in doing this

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    On windows:

    You can just use the WaitForMultipleObjects function passing the array of thereads' handles.
    If you set bWaitAll parameter to true, this function will exit when all threads, whose handles are present in the array, are finished.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    thanks vart but iam working on red hat linux can u suggest using semaphore functions

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    So post your attempt at using semaphores then.
    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.

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    In Linux (as I know) there is no semaphore type other than binary semaphores. You have mutex primitive in hand from pthread API. You have to implement your own counting semaphore type.

    I am sure that you can find any implementation of counting semaphores from binary semaphores.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Linux has arbitrary value semaphores. Look at the sem_* family of functions.

    But a semaphore is not the right tool for this job. vart's approach of actually waiting for thread termination is better. You can use pthread_join() for this if you use pthreads in the first place.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 01:54 AM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 12:45 PM
  3. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  4. Semaphores
    By Jules in forum C Programming
    Replies: 4
    Last Post: 01-18-2004, 01:58 PM
  5. Semaphores
    By edk in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2001, 03:55 PM