none
SetEvent and WaitForMultipleObjects taking more time in processing. RRS feed

  • Question

  • I have multiple processes dependent on signal from one process. If I'm doing the below operation with only 2 processes (A & B), it's faster. But, when I have more and more processes to run in synchronization, its taking too much of time.  It's like if a process takes 10msec for processing, when I'm running 5 processes, it takes 50msec. Is it supposed to behave like this..? Its very slow for my requirement.

    For Ex: 

    Process A create events, "Start_process_B" & "Start_process_C"
    and Process B wakes up with the event "Start_process_B" from Process A
    and Process C wakes up with the event "Start_process_C" from Process A

    When Process B and Process C are done, they use SetEvent to signal Process A.
    Process B signal Process A with "Donewith_process_B"
    Process C signal Process A with "Donewith_process_C"
    Process A waits on WaitForMultipleObjects for events from Process B and Process C before its signalling the events to them again.

    Code Sample
    -------------------------------Process A
    static HANDLE aEvent[10],bEvent[10];
    aEvent[0] = CreateEvent(NULL, FALSE, FALSE, TEXT("Start_process_B"));    // All Events created are Auto Reset Events
    aEvent[1] = CreateEvent(NULL, FALSE, FALSE, TEXT("Start_process_C")); 

    bEvent[0] = OpenEvent(SYNCHRONIZE, FALSE, TEXT("Donewith_process_B"));
    bEvent[1] = OpenEvent(SYNCHRONIZE, FALSE, TEXT("Donewith_process_c"));

    while(1)
    {
    for (int k = 0; k < 2; k++)
    {
    SetEvent(aEvent[k]);
    }                           

    WaitForMultipleObjects(2, bEvent, TRUE, INFINITE);

    }

    -------------------------------Process B
    static HANDLE aEvent, bEvent;
    aEvent = OpenEvent(SYNCHRONIZE, FALSE, TEXT("Start_process_B"));
    bEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("Donewith_process_B")); 

    while(1)
    {
    WaitForSingleObject(bEvent, INFINITE);
    ......do some processing here
    SetEvent(bEvent);    //Signal Process A of completion
    }


    -------------------------------Process C
    static HANDLE aEvent, cEvent;
    aEvent = OpenEvent(SYNCHRONIZE, FALSE, TEXT("Start_process_c"));
    cEvent = CreateEvent(NULL, FALSE, FALSE, TEXT("Donewith_process_c")); 

    while(1)
    {
    WaitForSingleObject(cEvent, INFINITE);
    ......do some processing here
    SetEvent(cEvent);    //Signal Process A of completion
    }

    System configuration 

    Intel Xeon E5 2660 server v4 2.0Ghz (2 processor) with Windows 10 OS(64 Bit)

    Wednesday, October 9, 2019 12:36 PM

Answers

  • Even if you prioritise the scheduling, if those processes are the only ones currently running on the system then that will make no difference. Since the processor that you mentioned has 28 threads of execution, what's the likelyhood of this being a priority issue when you can essentially run 56 threads at once?

    There are two things that I suggest you try.

    First, try this on a single processor system with less cores.

    Second, try setting all the processes so they will run on the same NUMA node.

    I suggest this because more cores and more processors doesn't always results in faster code.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by Harsha_MR Wednesday, October 9, 2019 6:55 PM
    Wednesday, October 9, 2019 3:08 PM

All replies

  • When an event is signaled it makes the waiting threads eligible to run, but it doesn't mean that they will be immediately scheduled.
    Wednesday, October 9, 2019 12:41 PM
  • I can't take such delay in my processing. When i add more and more processes to wait on events, it just adds up more time. Any solution to prioritize the scheduling..?
    • Edited by Harsha_MR Wednesday, October 9, 2019 1:13 PM
    Wednesday, October 9, 2019 1:13 PM
  • Even if you prioritise the scheduling, if those processes are the only ones currently running on the system then that will make no difference. Since the processor that you mentioned has 28 threads of execution, what's the likelyhood of this being a priority issue when you can essentially run 56 threads at once?

    There are two things that I suggest you try.

    First, try this on a single processor system with less cores.

    Second, try setting all the processes so they will run on the same NUMA node.

    I suggest this because more cores and more processors doesn't always results in faster code.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Marked as answer by Harsha_MR Wednesday, October 9, 2019 6:55 PM
    Wednesday, October 9, 2019 3:08 PM
  • I'm not sure. But for some reason only one processor was enabled for the processes.. making setaffinity to all processor gave me better result.. Thanks.. 
    Wednesday, October 9, 2019 6:56 PM