ÿþ<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=unicode" /> <meta http-equiv="Content-Language" content="en-us" /> <style> <!-- p.MsoNormal {margin-top:0mm; margin-right:0mm; margin-bottom:10.0pt; margin-left:0mm; line-height:115%; font-size:12.0pt; font-family:"Times New Roman","serif";} a:link {color:blue; text-decoration:underline;} a:visited {color:purple; text-decoration:underline;} p {margin-right:0mm; margin-left:0mm; font-size:12.0pt; font-family:"Times New Roman","serif";} --> </style> <title>The Windows thread synchronization tutorial: Win32 event synchronization objects</title> <meta name="keywords" content="system programming, technology, tools, computers, servers, processors, benchmarking, opensource, hardware" /> <meta name="description" content="The Win32 thread synchronization programming using the event synchronization objects" /> </head> <body lang="EN-US" link="#0000FF" vlink="#800080" topmargin="20" leftmargin="20" rightmargin="20" bottommargin="20"> <div class="Section1"> <h3 align="center" style="margin-bottom:0mm;margin-bottom:0; text-align:center; margin-top:0"><font size="5" face="Byington"> <span style="line-height:115%;font-weight:400">Windows Thread Synchronization 4</span></font></h3> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:0; margin-top:0">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt" align="center"> <script type="text/javascript"><!-- google_ad_client = "pub-8089415323104206"; google_ad_slot = "0761177910"; google_ad_width = 728; google_ad_height = 90; //--> </script> &nbsp;<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"><b> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%; font-weight:bold">Synchronization Objects</span></font></b></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> A synchronization object is an object whose handle can be specified in one of the wait functions to coordinate the execution of multiple threads. More than one process can have a handle to the same synchronization object, making interprocess synchronization possible. The following object types are provided exclusively for synchronization.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%">&nbsp;</span></font></p> <div align="center"> <table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none"> <tr> <td bgcolor="#D9D9D9" style="border:solid black 1.0pt;background:#D9D9D9; padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:.0001pt; text-align:center"><b><font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%;font-weight:bold"> Type</span></font></b></p> </td> <td bgcolor="#D9D9D9" style="border:solid black 1.0pt;border-left:none; background:#D9D9D9;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" align="center" style="margin-bottom:0mm;margin-bottom:.0001pt; text-align:center"><b><font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%;font-weight:bold"> Description</span></font></b></p> </td> </tr> <tr> <td style="border:solid black 1.0pt;border-top:none;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Event</span></font></p> </td> <td style="border-top:none;border-left:none;border-bottom:solid black 1.0pt; border-right:solid black 1.0pt;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Notifies one or more waiting threads that an event has occurred. Events are a way of signaling one thread from another, allowing one thread to wait or sleep until it s signaled by another thread.</span></font></p> </td> </tr> <tr> <td style="border:solid black 1.0pt;border-top:none;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Mutex</span></font></p> </td> <td style="border-top:none;border-left:none;border-bottom:solid black 1.0pt; border-right:solid black 1.0pt;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Can be owned by only one thread at a time, enabling threads to coordinate <b> <span style="font-weight:bold">mutually exclusive access</span></b> to a shared resource.</span></font></p> </td> </tr> <tr> <td style="border:solid black 1.0pt;border-top:none;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Semaphore</span></font></p> </td> <td style="border-top:none;border-left:none;border-bottom:solid black 1.0pt; border-right:solid black 1.0pt;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Maintains a count between zero and some maximum value, limiting the number of threads that are simultaneously accessing a shared resource. A semaphore is a mutex that multiple threads can access. It s like having multiple tokens. Mutex is exactly the same as a semaphore with semaphore value 1.</span></font></p> </td> </tr> <tr> <td style="border:solid black 1.0pt;border-top:none;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Waitable timer</span></font></p> </td> <td style="border-top:none;border-left:none;border-bottom:solid black 1.0pt; border-right:solid black 1.0pt;padding:0mm 5.4pt 0mm 5.4pt"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Notifies one or more waiting threads that a specified time has arrived.</span></font></p> </td> </tr> </table> </div> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%">&nbsp;</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"><b> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%; font-weight:bold">Events</span></font></b></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> Events are a way of signaling one thread from another, allowing one thread to wait or sleep until it s signaled by another thread. The following example shows two threads using an event: Thread A on the left is producing data, and Thread B on the right is consuming data.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt" align="center"> <font face="Arial"> <img width="540" height="190" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode005.png" alt="Windows thread synchronization: The event object" /></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> The producing thread, Thread A generates some data and puts it in a shared working space. In this example, the consuming thread, Thread B is sleeping on the event (waiting for the event to trigger).</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt" align="center"> <font face="Arial"> <img width="540" height="190" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode006.png" alt="Windows thread synchronization: Event triggered" /></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> Once the producing thread has finished writing data, it triggers the event.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt" align="center"> <font face="Arial"> <img width="540" height="190" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode007.png" alt="Windows thread synchronization: Telling the event the data already sent" /></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> This signals the consuming thread, Thread B, thereby waking it up.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt" align="center"> <font face="Arial"> <img width="540" height="190" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode008.png" alt="Windows thread synchronization: Event - telling other thread to use the sent data" /></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> Once the consuming thread, Thread B has woken up, it starts doing work. The assumption is that the producing thread will no longer touch the data.</span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt" align="center"> <font face="Arial"> <img width="540" height="190" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode009.png" alt="Windows thread synchronization: Event - other thread start using the data" /></font><font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> </span></font></p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt">&nbsp;</p> <h3 align="center" style="margin-top: 0; margin-bottom: 0"> <script type="text/javascript"><!-- google_ad_client = "pub-8089415323104206"; google_ad_slot = "2156170134"; google_ad_width = 728; google_ad_height = 15; //--> </script> &nbsp;<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script></h3> <p align="center" style="margin-top: 0; margin-bottom: 0">&nbsp;</p> <h3 align="center" style="margin-top: 0; margin-bottom: 0"> <font face="Byington"><span style="font-weight: 400">&lt; <a title="A Windows process and thread tutorial: the wait functions, single-object, multiple-object, alertable, registered wait functions etc" style="color: blue; text-decoration: underline" href="threadprocesssynchronizationapis11_2.html"> Thread Synchronization 3</a> | <a title="The Windows Win32 thread synchronization techniques programming tutorials with program examples and code samples" style="color: blue; text-decoration: underline" href="threadprocesssynchronizationapis11index.html"> Thread Synchronization Programming</a> | <a title="The Win32 programming tutorial using Visual Studio, C and C++ languages" style="color: blue; text-decoration: underline" href="index.html"> Win32 Programming</a> | <a title="The Windows Win32 thread synchronization tutorial: Mutex and Semaphore synchronization objects" style="color: blue; text-decoration: underline" href="threadprocesssynchronizationapis11_4.html"> Thread Synchronization 5</a> &gt;</span></font></h3> <p align="left" style="margin-top: 0; margin-bottom: 0">&nbsp;</p> <div align="center"> <script src="http://tag.contextweb.com/TagPublish/getjs.aspx?action=VIEWAD&cwrun=200&cwadformat=728X90&cwpid=527221&cwwidth=728&cwheight=90&cwpnet=1&cwtagid=82740"></script> </div> </div> </body> </html>