Windows System Programming Third Edition [Electronic resources]

Johnson M. Hart

نسخه متنی -صفحه : 291/ 124
نمايش فراداده

  • Thread Synchronization Objects

    Two mechanisms discussed so far allow processes and threads to synchronize with one another.

    1. A thread running in a process can wait for another process to terminate, using ExitProcess, by waiting on the process handle using WaitForSingleObject or WaitForMultipleObjects. A thread can wait for another thread to terminate (ExitThread or return) in the same way.

    2. File locks are specifically for synchronizing file access.

    Windows provides four other objects designed for thread and process synchronization. Three of these objectsmutexes, semaphores, and eventsare kernel objects that have handles. Events are also used for other purposes, such as asynchronous I/O (Chapter 14).

    The fourth object, the CRITICAL_SECTION, is discussed first. Because of their simplicity and performance advantages, CRITICAL_SECTIONs are the preferred mechanism when they are adequate for a program's requirements. There are some performance issues, however, which are described in Chapter 9.

    Caution: There are risks inherent to the use of synchronization objects if they are not used properly. These risks, such as deadlocks, are described in this and subsequent chapters, along with techniques for developing reliable code. First, however, we'll show some synchronization examples in realistic situations.

    Two other synchronization objects, waitable timers and I/O completion ports, are deferred until Chapter 14. Both these objects require the Windows asynchronous I/O techniques described in that chapter.