ÿþ<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 Win32 thread synchronization tutorial: Mutex and Semaphore synchronization objects</title> <meta name="keywords" content="hardware, microprocessors, technology, optimization, tools, projects, computers, benchmark, semaphore objects, mutex object, online" /> <meta name="description" content="Information on the Win32 mutex and semaphore synchronization objects for Windows thread synchronization" /> </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 5</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">Mutex and Semaphore</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 mutex works like a critical section. You can think of a mutex as a lock that must be grabbed before execution can continue. Here is an example of three threads that all want access to a shared resource.</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="383" height="216" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode010.png" alt="Windows thread synchronization: Mutex object" /></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%"> Each of the three threads tries to grab the mutex, but only one thread will be successful.</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="383" height="214" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode011.png" alt="Windows thread synchronization: Mutex object - any thread that have a mutex object can use the shared, protected resource" /></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%"> During the time that a thread holds the mutex, all other threads waiting on the mutex sleep. This behavior is very similar to that of a critical section.</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="367" height="214" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode012.png" alt="Windows thread synchronization: Mutex object - thread with mutex is using the shared, limited resource" /></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 a thread has finished using the shared resource, it releases the mutex. Another thread then can wakes up and grab the mutex.</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="476" height="185" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode013.png" alt="Windows thread synchronization: Mutex object - thread that finish using the limited, shared resource return the mutex for other thread" /></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%"> A semaphore is a mutex that multiple threads can access. It s like having multiple tokens. If only one token is available (others have been used), then semaphore is a mutex.</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="383" height="223" src="threadprocesssynchronizationapis11_files/winthreadprocesssynchronizationcode014.png" alt="Windows thread synchronization: Semaphore object - it is many mutexes" /></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%"> Though available for other uses, the following objects can also be used 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" width="1105"> <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"> Object</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" width="908"> <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%">Change notification</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" width="908"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Created by the FindFirstChangeNotification() function, its state is set to signaled when a specified type of change occurs within a specified directory or directory tree.</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%">Console input</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" width="908"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Created when a console is created. The handle to console input is returned by the CreateFile() function when CONIN$ is specified, or by the GetStdHandle() function. Its state is set to signaled when there is unread input in the console&#39;s input buffer, and set to nonsignaled when the input buffer is empty.</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%">Job</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" width="908"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Created by calling the CreateJobObject() function. The state of a job object is set to signaled when all its processes are terminated because the specified end-of-job time limit has been exceeded.</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%">Memory resource notification</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" width="908"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Created by the CreateMemoryResourceNotification() function. Its state is set to signaled when a specified type of change occurs within physical memory.</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%">Process</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" width="908"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Created by calling the CreateProcess() function. Its state is set to nonsignaled while the process is running, and set to signaled when the process terminates.</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%">Thread</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" width="908"> <p class="MsoNormal" style="margin-bottom:0mm;margin-bottom:.0001pt"> <font size="3" face="Arial"> <span style="font-size:12.0pt;line-height:115%">Created when a new thread is created by calling the CreateProcess(), CreateThread(), or CreateRemoteThread() function. Its state is set to nonsignaled while the thread is running, and set to signaled when the thread terminates.</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"> <font size="3" face="Arial"><span style="font-size:12.0pt;line-height:115%"> In some circumstances, you can also use a file, named pipe, or communications device as a synchronization object; however, their use for this purpose is discouraged. Instead, use asynchronous I/O and wait on the event object set in the OVERLAPPED structure. It is safer to use the event object because of the confusion that can occur when multiple <b><span style="font-weight:bold">simultaneous overlapped operations</span></b> are performed on the same file, named pipe, or communications device. In this situation, there is no way to know which operation caused the object&#39;s state to be signaled.</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="The Windows process and thread synchronization tutorial: Event synchronization objects" style="color: blue; text-decoration: underline" href="threadprocesssynchronizationapis11_3.html"> Thread Synchronization 4</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: Interprocess synchronization, object names, inheritance, duplication and overlapped input and output synchronization" style="color: blue; text-decoration: underline" href="threadprocesssynchronizationapis11_5.html"> Thread Synchronization 6</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>