Intel IA-32 Computer Accessories User Manual


 
Multi-Core and Hyper-Threading Technology 7
7-29
In general, OS function calls should be used with care when
synchronizing threads. When using OS-supported thread
synchronization objects (critical section, mutex, or semaphore),
preference should be given to the OS service that has the least
synchronization overhead, such as a critical section.
Example 7-5 Coding Pitfall using Spin Wait Loop
(a) A spin-wait loop attempts to release the processor incorrectly. It experiences a performance
penalty if the only worker thread and the control thread runs on the same physical processor
package.
// Only one worker thread is running,
// the control loop waits for the worker thread to complete.
ResumeWorkThread(thread_handle);
While (!task_not_done ) {
Sleep(0) // Returns immediately back to spin loop.
}
(b) A polling loop frees up the processor correctly.
// Let a worker thread run and wait for completion.
ResumeWorkThread(thread_handle);
While (!task_not_done ) {
Sleep(FIVE_MILISEC)
// This processor is released for some duration, the processor
// can be used by other threads.
}