Thread: waiting for semaphores

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    waiting for semaphores

    I'm creating a semaphore in my program's main window. The semaphore is created like this:
    if(!(d.hSemf= CreateSemaphore(0, THREAD_COUNT, THREAD_COUNT, 0)))
    return(0);

    In each thread that is created, i WaitForSingleObject(d->hSemf, INFINITE) to signal that it still is running.
    When the thread is closing, i ReleaseSemaphore(d->hSemf, 1, 0) to signal that it is closed.

    In the main window's WM_DESTROY, i try to wait for the threads to close by using the following code:
    Code:
    iI=0;
    while(iI<THREAD_COUNT){
      if((lRes=WaitForSingleObject(d.hSemf, 100))!=WAIT_OBJECT_0 && lRes!=WAIT_TIMEOUT)
      iI++;
    }
    This doesn't work, and i can't figure out why!?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    WAIT_OBJECT_0

    Why skip?

    Kuphryn

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Ok, a little typo..

    It still doesn't work though...

    I can wait for the seamaphore, and fetch it once.
    My idea is to have one object for all threads.
    The object is used to wait for all threads to terminate before a database connection is closed.
    Today i'm using mutexes, but this requires an array of mutexes, and a semaphore would be easier, since it's only one object that is decremented when a thread starts, and incremented when it stops.
    I thereby can wait in the main thread for the semaphore count to reach a certain count before terminating.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    All threads terminate?

    Kuphryn

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Figured it out.. The semaphore was closed a few lines before i tried to wait for it..

    Stupid error, but fixed now..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2009, 01:02 AM
  2. signal sending and waiting
    By eva69 in forum C Programming
    Replies: 1
    Last Post: 10-03-2008, 12:03 PM
  3. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  4. Getting input without waiting for keypress
    By Anon48 in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 02:18 AM
  5. Wait loop - not busy waiting?
    By Foldager in forum Windows Programming
    Replies: 1
    Last Post: 07-17-2003, 01:39 AM