Open topic with navigation
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:
- 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.
- 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.
- Call CreateThread with the CREATE_SUSPENDED flag to create a suspended thread having the function EventWaiter as the start function.
- Call SetThreadAffinityMask to restrict execution of the suspended thread to processor 1.
- Call ResumeThread to resume the suspended thread.
- In a loop that iterates 100 times perform the following algorithm:
- 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.
- Call RtSleep to sleep for 500 ms, which is enough time to guarantee that the other thread is waiting.
- Call QueryPerformanceCounter to obtain and store the current value of the performance counter. Call this value P1(Iter).
- 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.
- Call SetEvent to signal the event created in step #3.
- Call WaitForSingleObject to wait for the other thread to terminate.
- 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:
- Call WaitForSingleObject to wait until the event is signaled.
- Call QueryPerformanceCounter to obtain and store the current value of the performance counter. Call this value P2(Iter).
- 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.