Programming with Hyper-Threading Technology: How to Write Multithreaded Software for Intel IA-32 Processors

Waiting for Specific Events

So far this chapter has described two fundamental forms of waiting. Early on, you saw how to wait for threads to finish. Later, through the discussion of mutexes and critical sections, you saw how to wait for a section of code to be available. The one remaining synchronization point you will need in parallel programming, other than the completion of a thread, arises from waiting for an event to happen. For example, if several consumer threads are waiting for data, how do they know when the producer thread has generated it? You've already seen that spin waits are an extremely wasteful way of obtaining this notification, and certainly they should be avoided entirely when multiple threads are idling. What you need is a non-specific signal you can issue that enables a thread to begin work or resume working. Windows uses events for this purpose, while Pthreads uses condition variables.

Windows Events

In Windows, an event is an object that is either signaled or nonsignaled, on which other threads can wait. When the value is nonsignaled, the threads must wait. When its value is signaled, all threads waiting on the event can be scheduled by the operating system. Let's look at the code to see how this is done.

Creating an event requires us to call the CreateEvent() function:

HANDLE CreateEvent(        NULL,         // security attributes; here=no     ...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Thread Rolling Dies
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.