Replacement for Win32 events

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Replacement for Win32 events

    Hello,

    Beeing a Windows programmer I got very little experience with the unix api.
    From what I understood pthreads are needed for multithreaded programming. I
    looked at the pthread api and got the following question.

    I got a list of threads in my existing windows prog and during shutdown I
    got a event which will be signalled to specify the threads should be shut
    down.

    It looks like this (only a sample of how it looks):

    void ThreadFunc()
    {
    while (1)
    {
    if (waitforsingleobject(event, <return immediately>) == success) // If we
    got a event then we quit the loop and shut the thread
    break;
    }
    }


    ....
    for (i = 0; i < all threads; i++)
    {
    setevent(threadlist[i].event); // signal that we want to quit the thread
    }

    for (i = 0; i < all threads; i++)
    {
    if (waitforsingleobject(threadlist[i].event, <max. 500 millisecond
    delay>) == success) // if thread didn't shutdown we wait max. 500 msec and
    force it to be killed.
    terminate_thread(threadlist[i].event);
    }

    I assume I need to use some of those semaphore functions but I am not sure.
    I checked out some code which had both win32 and unix code in same code base
    but it didn't handle delaying like the 500 millisecond I did.

    Thanks in advance.

    -- John


    John Eskie Guest

  2. Similar Questions and Discussions

    1. Contribute replacement
      I've been having sporadic problems with connection and publishing errors spanning several users and websites. I've also perused this forum regularly...
    2. Win32::OLE, InternetExplorer, NewWindow, Events --- crashes
      I'm working on an application that automates IE and needs to handle IE's NewWindow2 event. The IE documentation for the event gives the following...
    3. Dave Roth's site (Win32::AdminMisc, Win32::ODBC, etc.) not available.
      Does anyone know of an alternate method to contact Dave Roth (other then rothd@roth.net )? It appears that his entire domain is unavailable...
    4. Win32-PerfMon on Win32
      Windows 2000(SP4) ActivePerl 5.8.3 I found this escapade rather confusing, I'm warning you now. I cannot install this Win32-PerfMon module, I...
    5. Is Win32::OLE qw(EVENTS) ~ to the VBA "Impliments" statement?
      Hey, I need to export a client interface into a Win32 COM server, to interact w/ an API. VB apparently does this by saying "Impliments foo" or...
  3. #2

    Default Re: Replacement for Win32 events


    "John Eskie" <cyberheg@l115.langkaer.dk> writes:
    > Hello,
    >
    > Beeing a Windows programmer I got very little experience with the unix api.
    > From what I understood pthreads are needed for multithreaded programming. I
    > looked at the pthread api and got the following question.
    >
    > I got a list of threads in my existing windows prog and during shutdown I
    > got a event which will be signalled to specify the threads should be shut
    > down.
    >
    > It looks like this (only a sample of how it looks):
    >
    > void ThreadFunc()
    > {
    > while (1)
    > {
    > if (waitforsingleobject(event, <return immediately>) == success) // If we
    > got a event then we quit the loop and shut the thread
    > break;
    > }
    > }
    >
    >
    > ...
    > for (i = 0; i < all threads; i++)
    > {
    > setevent(threadlist[i].event); // signal that we want to quit the thread
    > }
    >
    > for (i = 0; i < all threads; i++)
    > {
    > if (waitforsingleobject(threadlist[i].event, <max. 500 millisecond
    > delay>) == success) // if thread didn't shutdown we wait max. 500 msec and
    > force it to be killed.
    > terminate_thread(threadlist[i].event);
    > }
    >
    > I assume I need to use some of those semaphore functions but I am not sure.
    > I checked out some code which had both win32 and unix code in same code base
    > but it didn't handle delaying like the 500 millisecond I did.
    >
    > Thanks in advance.
    >
    > -- John
    I think that this could most easily be done using condition variables.
    Condition variables are used to signal state changes between threads,
    but they are different in that they don't hold any state themselves.
    (You can't test if a condition _was_ signalled, you can only wait for
    it to _be_ signalled.)

    So along with the condition variable, you need more data to represent
    the state that is changing, and a mutex to serialize shared access to
    that state data.

    The following enum defines the protocol between your master thread
    and the worker threads:

    typedef enum {
    ThrInit = 0, // Initial state,
    ThrShutdownReq, // Thread requested to shut down
    ThrShutdown // Thread has shut down.
    } ThrState_t;

    Next you need an array of these for all of your threads:

    ThrState_t ThrState[MAX_THREADS];

    We can use one mutex to protect the whole array:

    pthread_mutex_t ThrStateMutex = PTHREAD_MUTEX_INITIALIZER;

    And we can use one condition variable too:

    pthread_cond_t ThrStateCond = PTHREAD_COND_INITIALIZER;

    A thread can wait for the master to signal the ThrShutdownReq
    state like so:

    pthread_mutex_lock(&ThrStateMutex);

    // Wait for the shutdown request
    //
    while (ThrState[my_thread_num] != ThrShutdownReq)
    if ((rc = pthread_cond_wait(&ThrStateCond, &ThrStateMutex)) != 0)
    break;

    // Indicate that we've shut down.
    // Should also catch errors when rc != 0
    //
    ThrState[my_thread_num] = ThrShutdown

    // Broadcast the state change so the master thread will see it.
    //
    pthread_cond_broadcast(&ThrStateCond);

    pthread_mutex_unlock(&ThrStateMutex);


    The master thread can shutdown the thread number thread_num
    in this way:


    pthread_mutex_lock(&ThrStateMutex);

    // Indicate that the thread should shut down.
    //
    ThrState[thread_num] = ThrShutdownReq;

    // Broadcast the state change so the thread will see it.
    //
    pthread_cond_broadcast(&ThrStateCond);

    // Wait for the thread shutdown to complete.
    //
    while (ThrState[thread_num] != ThrShutdown)
    if ((rc = pthread_cond_wait(&ThrStateCond, &ThrStateMutex)) != 0)
    break;

    pthread_mutex_unlock(&ThrStateMutex);


    If you want to have a timeout on your wait, then you would
    use pthread_cond_timedwait().

    Note that we since we are sharing a single mutex-condvar pair
    among all threads, we must use pthread_cond_broadcast to wake
    all of the threads that are waiting on the ThrStateCond.
    This design produces needless wakeups, but it should not be
    a performance issue. If it is, you could allocate separate
    condvar-mutex pairs for each worker thread.

    Note that even if you dedicate separate condvars, you must
    still test explicitly for the state change on each return
    from pthread_cond_wait(), since POSIX allows it to return
    "spuriously" - even when it hasn't been signaled.

    Hope this helps. Adding comp.programming.threads to followups.

    -SEan

    --
    Remove the pi to email me.
    Sean Burke Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139