SimpleSMPLatency

Description

This sample demonstrates how to measure inter-thread latency when synchronizing threads using an event. This sample must be run on a system with at least 2 processors. When built using Debug or Release configurations, this sample will call only Win32 functions and not RTAPI functions.

This application contains two functions: a main function and EventWaiter. The main function of this application performs the following algorithm:

  1. Call RtGetProcessAffinityMask to get the system affinity mask. If there is only one processor in the system affinity mask, an error will be reported and the application will terminate.
  2. Call RtCreateEvent to create an event object. The handle to the event will be stored in a static file-scope variable so that both the main thread and the thread spawned in the next step can access it.
  3. Call CreateThread with the CREATE_SUSPENDED flag to create a suspended thread having the function EventWaiter as the start function.
  4. Call SetThreadAffinityMask to restrict execution of the suspended thread to processor 1.
  5. Call ResumeThread to resume the suspended thread.
  6. In a loop that iterates 100 times perform the following algorithm:
  7. Call Iter the number of the current iteration of this loop, starting at 0. This value will be stored in a static file-scope variable so that the other thread can access it.
  8. Call RtSleep to sleep for 500 ms, which is enough time to guarantee that the other thread is waiting.
  9. Call QueryPerformanceCounter to obtain and store the current value of the performance counter. Call this value P1(Iter).
  10. If this is the last iteration of this loop, set Terminate, a static file-scope flag, to true to indicate that the other thread should terminate after processing the next signal of the event.
  11. Call SetEvent to signal the event created in step #3.
  12. Call WaitForSingleObject to wait for the other thread to terminate.
  13. Use the values P1(0..99) and P2(0..99) to compute and display the average, minimum, and maximum latencies from the above loop. Optionally display the entire table of P1/P2 values. If the latency values are to be displayed in time units rather than performance counter units, a call to QueryPerformanceFrequency will be necessary.

Function EventWaiter performs the following algorithm in an endless loop:

  1. Call WaitForSingleObject to wait until the event is signaled.
  2. Call QueryPerformanceCounter to obtain and store the current value of the performance counter. Call this value P2(Iter).
  3. If the flag Terminate is true, return.

Running the Sample

Run the application. It will output the latency measurement data that it collects.

Source Files

SimpleSMPLatency.vcxproj Viisual Studio project source file.

IntervalZero.com | Support | Give Feedback