Multiple instances of an object with WaitForMultipleObjects function inside - Windows NT

This is a discussion on Multiple instances of an object with WaitForMultipleObjects function inside - Windows NT ; I'm new to using WaitForMultipleObjects in an OO program, would really appreciate if anyone can answer my question. I have a WaitForMultipleObjects function in an object. This function is listening to two events and should return when one of the ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Multiple instances of an object with WaitForMultipleObjects function inside

  1. Multiple instances of an object with WaitForMultipleObjects function inside

    I'm new to using WaitForMultipleObjects in an OO program, would really
    appreciate if anyone can answer my question.

    I have a WaitForMultipleObjects function in an object. This function
    is listening to two events and should return when one of the events
    get signaled. Somehow for my application, I know that after one event
    gets signaled, the other one would almost instantly gets signaled as
    well.

    If I have only one instance of the application, the program can exit
    graciously. However, when I create two instances of the objects, some
    weird things happen. Let's say I have InstA and InstB objects. InstA
    created ThreadA1 and ThreadA2 while InstB created ThreadB1 and
    ThreadB2. ThreadA1 signals the event, and InstA exits okay even
    though ThreadA2 actually tries to signal an event. The funny thing
    comes then as WaitForMultipleObjects of InstB somehow gets signal from
    ThreadA2 while ThreadB1 and ThreadB2 haven't signaled anything yet. I
    can't think of why it does that nor how I could fix the problem. Does
    anyone see similar thing before?

    Thanks,
    Wan

  2. Re: Multiple instances of an object with WaitForMultipleObjects function inside

    In message , Wan
    writes
    >I'm new to using WaitForMultipleObjects in an OO program, would really
    >appreciate if anyone can answer my question.
    >
    >I have a WaitForMultipleObjects function in an object. This function
    >is listening to two events and should return when one of the events
    >get signaled. Somehow for my application, I know that after one event
    >gets signaled, the other one would almost instantly gets signaled as
    >well.
    >
    >If I have only one instance of the application, the program can exit
    >graciously. However, when I create two instances of the objects, some
    >weird things happen. Let's say I have InstA and InstB objects. InstA
    >created ThreadA1 and ThreadA2 while InstB created ThreadB1 and
    >ThreadB2. ThreadA1 signals the event, and InstA exits okay even
    >though ThreadA2 actually tries to signal an event. The funny thing
    >comes then as WaitForMultipleObjects of InstB somehow gets signal from
    >ThreadA2 while ThreadB1 and ThreadB2 haven't signaled anything yet. I
    >can't think of why it does that nor how I could fix the problem. Does
    >anyone see similar thing before?
    >
    >Thanks,
    >Wan


    I'm not quite sure what the problem is. Do you want InstA to detect
    that ThreadA1 *and* ThreadA2 have signalled or do you want InstB *not*
    to detect that ThreadA2 has signalled (or both of these)?

    If the former, I put cross-checking for other events following the
    return from WaitForMultipleObjects(). By that I mean that I would
    WaitForSingleObject() for the other event but with zero timeout
    interval.

    If the latter, is this happening because the ThreadA1 and ThreadB1
    events have the same name? Do they have to have the same name? Or a
    name at all, for that matter?

    Can you post some bits of code, it may help?

    --
    Fenster

  3. Re: Multiple instances of an object with WaitForMultipleObjects function inside

    Fenster wrote in message news:<+lKWVmCxPO7AFwnm@FensterPC.croctec.co.uk>...
    > In message , Wan
    > writes
    > >I'm new to using WaitForMultipleObjects in an OO program, would really
    > >appreciate if anyone can answer my question.
    > >
    > >I have a WaitForMultipleObjects function in an object. This function
    > >is listening to two events and should return when one of the events
    > >get signaled. Somehow for my application, I know that after one event
    > >gets signaled, the other one would almost instantly gets signaled as
    > >well.
    > >
    > >If I have only one instance of the application, the program can exit
    > >graciously. However, when I create two instances of the objects, some
    > >weird things happen. Let's say I have InstA and InstB objects. InstA
    > >created ThreadA1 and ThreadA2 while InstB created ThreadB1 and
    > >ThreadB2. ThreadA1 signals the event, and InstA exits okay even
    > >though ThreadA2 actually tries to signal an event. The funny thing
    > >comes then as WaitForMultipleObjects of InstB somehow gets signal from
    > >ThreadA2 while ThreadB1 and ThreadB2 haven't signaled anything yet. I
    > >can't think of why it does that nor how I could fix the problem. Does
    > >anyone see similar thing before?
    > >
    > >Thanks,
    > >Wan

    >
    > I'm not quite sure what the problem is. Do you want InstA to detect
    > that ThreadA1 *and* ThreadA2 have signalled or do you want InstB *not*
    > to detect that ThreadA2 has signalled (or both of these)?
    >
    > If the former, I put cross-checking for other events following the
    > return from WaitForMultipleObjects(). By that I mean that I would
    > WaitForSingleObject() for the other event but with zero timeout
    > interval.
    >
    > If the latter, is this happening because the ThreadA1 and ThreadB1
    > events have the same name? Do they have to have the same name? Or a
    > name at all, for that matter?
    >
    > Can you post some bits of code, it may help?


    I would like InstB not to detect the ThreadA2 at all. So the latter
    case is what I want.

    The threads are created in different objects so they should have
    different HANDLE values(my guess). I have some of my code down here.

    HANDLE hEvent[2], hThread[2];
    DWORD IDThread;
    hEvent[0] = CreateEvent(
    NULL, // no security attributes
    FALSE, // auto-reset event
    FALSE, // initial state is NOT signaled
    NULL); // object name

    if (hEvent[1] == NULL)
    {
    return;
    }
    hThread[0] = CreateThread(
    NULL,
    0,
    (LPTHREAD_START_ROUTINE)func1,
    &param1, // pass event handle
    0,
    &IDThread);
    if (hThread[0] == NULL)
    {
    return;
    }
    hEvent[1] = CreateEvent(
    NULL, // no security attributes
    FALSE, // auto-reset event
    FALSE, // initial state is NOT signaled
    NULL); // object name

    if (hEvent[1] == NULL)
    {
    return;
    }

    hThread[1] = CreateThread(
    NULL,
    0,
    (LPTHREAD_START_ROUTINE)func2,
    &param2, // pass event handle
    0,
    &IDThread);
    if (hThread[1] == NULL)
    {
    return;
    }

    DWORD dwWaitResult = -1;
    dwWaitResult = WaitForMultipleObjects(
    2, // number of handles in array
    hEvent, // array of read-event handles
    FALSE, // wait until all are signaled
    INFINITE); // indefinite wait



    switch (dwWaitResult)
    {
    // All read-event objects were signaled.
    case WAIT_OBJECT_0 + 0:
    TRACE_DATA("func1 returned");
    break;

    case WAIT_OBJECT_0 + 1:
    TRACE_DATA("func2 returned");
    break;
    }

    If I have one object that runs the above code and func1 returns first,
    then the case WAIT_OBJECT_0 + 0 would get executed which is okay. The
    program then exits fine. Then I move on to have two objects(InstA and
    InstB with func1 of InstA returns at a different time from func1 of
    InstB, the same holds true for func2). So in case func1 of InstA
    returns a bit faster than func2 of InstA, case 0 gets executed and
    InstA will exit. However, InstB is still running and none of its func
    has returned yet. And somehow it would catch the func2 return of InstA
    and treats it like a return of func2 of InstB. Is it making sense? I
    thought the funcs in different objects though they have same names
    should have different handles and InstB really shouldn't pick up the
    signal from InstA's func.

    Thanks a lot,
    Wan

  4. Re: Multiple instances of an object with WaitForMultipleObjects function inside


    See comments below.


    In message , Wan
    writes
    >Fenster wrote in message
    >news:<+lKWVmCxPO7AFwnm@FensterPC.croctec.co.uk>...
    >> In message , Wan
    >> writes
    >> >I'm new to using WaitForMultipleObjects in an OO program, would really
    >> >appreciate if anyone can answer my question.
    >> >
    >> >I have a WaitForMultipleObjects function in an object. This function
    >> >is listening to two events and should return when one of the events
    >> >get signaled. Somehow for my application, I know that after one event
    >> >gets signaled, the other one would almost instantly gets signaled as
    >> >well.
    >> >
    >> >If I have only one instance of the application, the program can exit
    >> >graciously. However, when I create two instances of the objects, some
    >> >weird things happen. Let's say I have InstA and InstB objects. InstA
    >> >created ThreadA1 and ThreadA2 while InstB created ThreadB1 and
    >> >ThreadB2. ThreadA1 signals the event, and InstA exits okay even
    >> >though ThreadA2 actually tries to signal an event. The funny thing
    >> >comes then as WaitForMultipleObjects of InstB somehow gets signal from
    >> >ThreadA2 while ThreadB1 and ThreadB2 haven't signaled anything yet. I
    >> >can't think of why it does that nor how I could fix the problem. Does
    >> >anyone see similar thing before?
    >> >
    >> >Thanks,
    >> >Wan

    >>
    >> I'm not quite sure what the problem is. Do you want InstA to detect
    >> that ThreadA1 *and* ThreadA2 have signalled or do you want InstB *not*
    >> to detect that ThreadA2 has signalled (or both of these)?
    >>
    >> If the former, I put cross-checking for other events following the
    >> return from WaitForMultipleObjects(). By that I mean that I would
    >> WaitForSingleObject() for the other event but with zero timeout
    >> interval.
    >>
    >> If the latter, is this happening because the ThreadA1 and ThreadB1
    >> events have the same name? Do they have to have the same name? Or a
    >> name at all, for that matter?
    >>
    >> Can you post some bits of code, it may help?

    >
    >I would like InstB not to detect the ThreadA2 at all. So the latter
    >case is what I want.
    >
    >The threads are created in different objects so they should have
    >different HANDLE values(my guess). I have some of my code down here.
    >
    > HANDLE hEvent[2], hThread[2];
    > DWORD IDThread;
    > hEvent[0] = CreateEvent(
    > NULL, // no security attributes
    > FALSE, // auto-reset event
    > FALSE, // initial state is NOT signaled
    > NULL); // object name
    >
    > if (hEvent[1] == NULL)
    > {
    > return;
    > }


    I assume that it's a typo here that you've created hEvent[0] but are
    comparing hEvent[1] against NULL?

    Also, maybe you should call GetLastError() in case it returns
    ERROR_ALREADY_EXISTS.

    > hThread[0] = CreateThread(
    > NULL,
    > 0,
    > (LPTHREAD_START_ROUTINE)func1,
    > &param1, // pass event handle
    > 0,
    > &IDThread);


    Again, I assume that it's a typo here that param1 doesn't appear to be
    initialised (or even exist).

    > if (hThread[0] == NULL)
    > {
    > return;
    > }
    > hEvent[1] = CreateEvent(
    > NULL, // no security attributes
    > FALSE, // auto-reset event
    > FALSE, // initial state is NOT signaled
    > NULL); // object name
    >
    > if (hEvent[1] == NULL)
    > {
    > return;
    > }
    >
    > hThread[1] = CreateThread(
    > NULL,
    > 0,
    > (LPTHREAD_START_ROUTINE)func2,
    > &param2, // pass event handle
    > 0,
    > &IDThread);


    Again, I assume that it's a typo here that param2 doesn't appear to be
    initialised (or even exist).

    > if (hThread[1] == NULL)
    > {
    > return;
    > }
    >
    > DWORD dwWaitResult = -1;
    > dwWaitResult = WaitForMultipleObjects(
    > 2, // number of handles in array
    > hEvent, // array of read-event handles
    > FALSE, // wait until all are signaled
    > INFINITE); // indefinite wait
    >
    >
    >
    > switch (dwWaitResult)
    > {
    > // All read-event objects were signaled.
    > case WAIT_OBJECT_0 + 0:
    > TRACE_DATA("func1 returned");
    > break;
    >
    > case WAIT_OBJECT_0 + 1:
    > TRACE_DATA("func2 returned");
    > break;
    > }
    >
    >If I have one object that runs the above code and func1 returns first,
    >then the case WAIT_OBJECT_0 + 0 would get executed which is okay. The
    >program then exits fine. Then I move on to have two objects(InstA and
    >InstB with func1 of InstA returns at a different time from func1 of
    >InstB, the same holds true for func2). So in case func1 of InstA
    >returns a bit faster than func2 of InstA, case 0 gets executed and
    >InstA will exit. However, InstB is still running and none of its func
    >has returned yet. And somehow it would catch the func2 return of InstA
    >and treats it like a return of func2 of InstB. Is it making sense? I
    >thought the funcs in different objects though they have same names
    >should have different handles and InstB really shouldn't pick up the
    >signal from InstA's func.
    >
    >Thanks a lot,
    >Wan


    If GetLastError() returns ERROR_ALREADY_EXISTS when the events are
    created the problem would appear to be related to how Windows identifies
    unnamed events internally and I have no idea how or where to find this.

    It may leave you having to generate unique names at run time which,
    presumably, wouldn't be too difficult.

    --
    Fenster

  5. Re: Multiple instances of an object with WaitForMultipleObjects function inside


    Sorry, I should have tried this before.

    I ran two instances of the following:

    #include

    #include
    #include

    int main(void)
    {
    HANDLE hEvent = CreateEvent(NULL, // No security attributes
    FALSE, // Auto-reset event
    FALSE, // Not initially signalled
    NULL); // Object name
    if (NULL == hEvent)
    {
    (void)printf("Could not create unnamed event\r\n");

    return(0);
    }

    (void)printf("Handle\t= 0x%X\r\n", hEvent);

    (void)printf("Error\t= %u\r\n", GetLastError());

    (void)printf("Press any key to continue...");

    while (0 == _kbhit())
    {
    }

    int nKey = _getch();
    if ((0 == nKey) || (0xE0 == nKey))
    {
    (void)_getch();
    }

    if (0 == CloseHandle(hEvent))
    {
    (void)printf("\r\nCloseHandle() failed\r\n");
    }

    (void)printf("\r\nError\t= %u\r\n", GetLastError());

    return(0);
    }

    Both instances pause at "Press any key to continue..." and the value of
    the handle is the same for both! It looks like unnamed events is not
    the way to go.



    In message , Fenster
    writes
    >
    >See comments below.
    >
    >
    >In message , Wan
    > writes
    >>Fenster wrote in message
    >>news:<+lKWVmCxPO7AFwnm@FensterPC.croctec.co.uk>...
    >>> In message , Wan
    >>> writes
    >>> >I'm new to using WaitForMultipleObjects in an OO program, would really
    >>> >appreciate if anyone can answer my question.
    >>> >
    >>> >I have a WaitForMultipleObjects function in an object. This function
    >>> >is listening to two events and should return when one of the events
    >>> >get signaled. Somehow for my application, I know that after one event
    >>> >gets signaled, the other one would almost instantly gets signaled as
    >>> >well.
    >>> >
    >>> >If I have only one instance of the application, the program can exit
    >>> >graciously. However, when I create two instances of the objects, some
    >>> >weird things happen. Let's say I have InstA and InstB objects. InstA
    >>> >created ThreadA1 and ThreadA2 while InstB created ThreadB1 and
    >>> >ThreadB2. ThreadA1 signals the event, and InstA exits okay even
    >>> >though ThreadA2 actually tries to signal an event. The funny thing
    >>> >comes then as WaitForMultipleObjects of InstB somehow gets signal from
    >>> >ThreadA2 while ThreadB1 and ThreadB2 haven't signaled anything yet. I
    >>> >can't think of why it does that nor how I could fix the problem. Does
    >>> >anyone see similar thing before?
    >>> >
    >>> >Thanks,
    >>> >Wan
    >>>
    >>> I'm not quite sure what the problem is. Do you want InstA to detect
    >>> that ThreadA1 *and* ThreadA2 have signalled or do you want InstB *not*
    >>> to detect that ThreadA2 has signalled (or both of these)?
    >>>
    >>> If the former, I put cross-checking for other events following the
    >>> return from WaitForMultipleObjects(). By that I mean that I would
    >>> WaitForSingleObject() for the other event but with zero timeout
    >>> interval.
    >>>
    >>> If the latter, is this happening because the ThreadA1 and ThreadB1
    >>> events have the same name? Do they have to have the same name? Or a
    >>> name at all, for that matter?
    >>>
    >>> Can you post some bits of code, it may help?

    >>
    >>I would like InstB not to detect the ThreadA2 at all. So the latter
    >>case is what I want.
    >>
    >>The threads are created in different objects so they should have
    >>different HANDLE values(my guess). I have some of my code down here.
    >>
    >> HANDLE hEvent[2], hThread[2];
    >> DWORD IDThread;
    >> hEvent[0] = CreateEvent(
    >> NULL, // no security attributes
    >> FALSE, // auto-reset event
    >> FALSE, // initial state is NOT signaled
    >> NULL); // object name
    >>
    >> if (hEvent[1] == NULL)
    >> {
    >> return;
    >> }

    >
    >I assume that it's a typo here that you've created hEvent[0] but are
    >comparing hEvent[1] against NULL?
    >
    >Also, maybe you should call GetLastError() in case it returns
    >ERROR_ALREADY_EXISTS.
    >
    >> hThread[0] = CreateThread(
    >> NULL,
    >> 0,
    >> (LPTHREAD_START_ROUTINE)func1,
    >> &param1, // pass event handle
    >> 0,
    >> &IDThread);

    >
    >Again, I assume that it's a typo here that param1 doesn't appear to be
    >initialised (or even exist).
    >
    >> if (hThread[0] == NULL)
    >> {
    >> return;
    >> }
    >> hEvent[1] = CreateEvent(
    >> NULL, // no security attributes
    >> FALSE, // auto-reset event
    >> FALSE, // initial state is NOT signaled
    >> NULL); // object name
    >>
    >> if (hEvent[1] == NULL)
    >> {
    >> return;
    >> }
    >>
    >> hThread[1] = CreateThread(
    >> NULL,
    >> 0,
    >> (LPTHREAD_START_ROUTINE)func2,
    >> &param2, // pass event handle
    >> 0,
    >> &IDThread);

    >
    >Again, I assume that it's a typo here that param2 doesn't appear to be
    >initialised (or even exist).
    >
    >> if (hThread[1] == NULL)
    >> {
    >> return;
    >> }
    >>
    >> DWORD dwWaitResult = -1;
    >> dwWaitResult = WaitForMultipleObjects(
    >> 2, // number of handles in array
    >> hEvent, // array of read-event handles
    >> FALSE, // wait until all are signaled
    >> INFINITE); // indefinite wait
    >>
    >>
    >>
    >> switch (dwWaitResult)
    >> {
    >> // All read-event objects were signaled.
    >> case WAIT_OBJECT_0 + 0:
    >> TRACE_DATA("func1 returned");
    >> break;
    >>
    >> case WAIT_OBJECT_0 + 1:
    >> TRACE_DATA("func2 returned");
    >> break;
    >> }
    >>
    >>If I have one object that runs the above code and func1 returns first,
    >>then the case WAIT_OBJECT_0 + 0 would get executed which is okay. The
    >>program then exits fine. Then I move on to have two objects(InstA and
    >>InstB with func1 of InstA returns at a different time from func1 of
    >>InstB, the same holds true for func2). So in case func1 of InstA
    >>returns a bit faster than func2 of InstA, case 0 gets executed and
    >>InstA will exit. However, InstB is still running and none of its func
    >>has returned yet. And somehow it would catch the func2 return of InstA
    >>and treats it like a return of func2 of InstB. Is it making sense? I
    >>thought the funcs in different objects though they have same names
    >>should have different handles and InstB really shouldn't pick up the
    >>signal from InstA's func.
    >>
    >>Thanks a lot,
    >>Wan

    >
    >If GetLastError() returns ERROR_ALREADY_EXISTS when the events are
    >created the problem would appear to be related to how Windows
    >identifies unnamed events internally and I have no idea how or where to
    >find this.
    >
    >It may leave you having to generate unique names at run time which,
    >presumably, wouldn't be too difficult.
    >


    --
    Fenster

  6. Re: Multiple instances of an object with WaitForMultipleObjects function inside

    Fenster, thanks for looking through my code and suggesting the
    possible solution of it. Sorry I had typos for some of it as i had to
    modify some names before I put it up here. For the param1 and param2,
    they are actually some objects of another class which I don't bother
    to write out everything...

    I tried to generate some random numbers in the program and attach that
    to the object name parameters for the CreateEvent. It seems to solve
    my problem. Thanks for your help! =)

    Wan
    Fenster wrote in message news:...
    > Sorry, I should have tried this before.
    >
    > I ran two instances of the following:
    >
    > #include
    >
    > #include
    > #include
    >
    > int main(void)
    > {
    > HANDLE hEvent = CreateEvent(NULL, // No security attributes
    > FALSE, // Auto-reset event
    > FALSE, // Not initially signalled
    > NULL); // Object name
    > if (NULL == hEvent)
    > {
    > (void)printf("Could not create unnamed event\r\n");
    >
    > return(0);
    > }
    >
    > (void)printf("Handle\t= 0x%X\r\n", hEvent);
    >
    > (void)printf("Error\t= %u\r\n", GetLastError());
    >
    > (void)printf("Press any key to continue...");
    >
    > while (0 == _kbhit())
    > {
    > }
    >
    > int nKey = _getch();
    > if ((0 == nKey) || (0xE0 == nKey))
    > {
    > (void)_getch();
    > }
    >
    > if (0 == CloseHandle(hEvent))
    > {
    > (void)printf("\r\nCloseHandle() failed\r\n");
    > }
    >
    > (void)printf("\r\nError\t= %u\r\n", GetLastError());
    >
    > return(0);
    > }
    >
    > Both instances pause at "Press any key to continue..." and the value of
    > the handle is the same for both! It looks like unnamed events is not
    > the way to go.
    >
    >
    >
    > In message , Fenster
    > writes
    > >
    > >See comments below.
    > >
    > >
    > >In message , Wan
    > > writes
    > >>Fenster wrote in message
    > >>news:<+lKWVmCxPO7AFwnm@FensterPC.croctec.co.uk>...
    > >>> In message , Wan
    > >>> writes
    > >>> >I'm new to using WaitForMultipleObjects in an OO program, would really
    > >>> >appreciate if anyone can answer my question.
    > >>> >
    > >>> >I have a WaitForMultipleObjects function in an object. This function
    > >>> >is listening to two events and should return when one of the events
    > >>> >get signaled. Somehow for my application, I know that after one event
    > >>> >gets signaled, the other one would almost instantly gets signaled as
    > >>> >well.
    > >>> >
    > >>> >If I have only one instance of the application, the program can exit
    > >>> >graciously. However, when I create two instances of the objects, some
    > >>> >weird things happen. Let's say I have InstA and InstB objects. InstA
    > >>> >created ThreadA1 and ThreadA2 while InstB created ThreadB1 and
    > >>> >ThreadB2. ThreadA1 signals the event, and InstA exits okay even
    > >>> >though ThreadA2 actually tries to signal an event. The funny thing
    > >>> >comes then as WaitForMultipleObjects of InstB somehow gets signal from
    > >>> >ThreadA2 while ThreadB1 and ThreadB2 haven't signaled anything yet. I
    > >>> >can't think of why it does that nor how I could fix the problem. Does
    > >>> >anyone see similar thing before?
    > >>> >
    > >>> >Thanks,
    > >>> >Wan
    > >>>
    > >>> I'm not quite sure what the problem is. Do you want InstA to detect
    > >>> that ThreadA1 *and* ThreadA2 have signalled or do you want InstB *not*
    > >>> to detect that ThreadA2 has signalled (or both of these)?
    > >>>
    > >>> If the former, I put cross-checking for other events following the
    > >>> return from WaitForMultipleObjects(). By that I mean that I would
    > >>> WaitForSingleObject() for the other event but with zero timeout
    > >>> interval.
    > >>>
    > >>> If the latter, is this happening because the ThreadA1 and ThreadB1
    > >>> events have the same name? Do they have to have the same name? Or a
    > >>> name at all, for that matter?
    > >>>
    > >>> Can you post some bits of code, it may help?
    > >>
    > >>I would like InstB not to detect the ThreadA2 at all. So the latter
    > >>case is what I want.
    > >>
    > >>The threads are created in different objects so they should have
    > >>different HANDLE values(my guess). I have some of my code down here.
    > >>
    > >> HANDLE hEvent[2], hThread[2];
    > >> DWORD IDThread;
    > >> hEvent[0] = CreateEvent(
    > >> NULL, // no security attributes
    > >> FALSE, // auto-reset event
    > >> FALSE, // initial state is NOT signaled
    > >> NULL); // object name
    > >>
    > >> if (hEvent[1] == NULL)
    > >> {
    > >> return;
    > >> }

    > >
    > >I assume that it's a typo here that you've created hEvent[0] but are
    > >comparing hEvent[1] against NULL?
    > >
    > >Also, maybe you should call GetLastError() in case it returns
    > >ERROR_ALREADY_EXISTS.
    > >
    > >> hThread[0] = CreateThread(
    > >> NULL,
    > >> 0,
    > >> (LPTHREAD_START_ROUTINE)func1,
    > >> &param1, // pass event handle
    > >> 0,
    > >> &IDThread);

    > >
    > >Again, I assume that it's a typo here that param1 doesn't appear to be
    > >initialised (or even exist).
    > >
    > >> if (hThread[0] == NULL)
    > >> {
    > >> return;
    > >> }
    > >> hEvent[1] = CreateEvent(
    > >> NULL, // no security attributes
    > >> FALSE, // auto-reset event
    > >> FALSE, // initial state is NOT signaled
    > >> NULL); // object name
    > >>
    > >> if (hEvent[1] == NULL)
    > >> {
    > >> return;
    > >> }
    > >>
    > >> hThread[1] = CreateThread(
    > >> NULL,
    > >> 0,
    > >> (LPTHREAD_START_ROUTINE)func2,
    > >> &param2, // pass event handle
    > >> 0,
    > >> &IDThread);

    > >
    > >Again, I assume that it's a typo here that param2 doesn't appear to be
    > >initialised (or even exist).
    > >
    > >> if (hThread[1] == NULL)
    > >> {
    > >> return;
    > >> }
    > >>
    > >> DWORD dwWaitResult = -1;
    > >> dwWaitResult = WaitForMultipleObjects(
    > >> 2, // number of handles in array
    > >> hEvent, // array of read-event handles
    > >> FALSE, // wait until all are signaled
    > >> INFINITE); // indefinite wait
    > >>
    > >>
    > >>
    > >> switch (dwWaitResult)
    > >> {
    > >> // All read-event objects were signaled.
    > >> case WAIT_OBJECT_0 + 0:
    > >> TRACE_DATA("func1 returned");
    > >> break;
    > >>
    > >> case WAIT_OBJECT_0 + 1:
    > >> TRACE_DATA("func2 returned");
    > >> break;
    > >> }
    > >>
    > >>If I have one object that runs the above code and func1 returns first,
    > >>then the case WAIT_OBJECT_0 + 0 would get executed which is okay. The
    > >>program then exits fine. Then I move on to have two objects(InstA and
    > >>InstB with func1 of InstA returns at a different time from func1 of
    > >>InstB, the same holds true for func2). So in case func1 of InstA
    > >>returns a bit faster than func2 of InstA, case 0 gets executed and
    > >>InstA will exit. However, InstB is still running and none of its func
    > >>has returned yet. And somehow it would catch the func2 return of InstA
    > >>and treats it like a return of func2 of InstB. Is it making sense? I
    > >>thought the funcs in different objects though they have same names
    > >>should have different handles and InstB really shouldn't pick up the
    > >>signal from InstA's func.
    > >>
    > >>Thanks a lot,
    > >>Wan

    > >
    > >If GetLastError() returns ERROR_ALREADY_EXISTS when the events are
    > >created the problem would appear to be related to how Windows
    > >identifies unnamed events internally and I have no idea how or where to
    > >find this.
    > >
    > >It may leave you having to generate unique names at run time which,
    > >presumably, wouldn't be too difficult.
    > >


+ Reply to Thread