semaphores help

This is a discussion on semaphores help within the Linux Programming forums, part of the Platform Specific Boards category; i created 11 threads..... and each thread is doing a function the 11th thread should execute after all the 10 ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,470
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    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,893
    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, 12: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, 12:58 PM
  5. Semaphores
    By edk in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2001, 02:55 PM

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