<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en-us" xml:lang="en-us"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="security" content="public" /> <meta name="Robots" content="index,follow" /> <meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' /> <meta name="DC.Type" content="concept" /> <meta name="DC.Title" content="Synchronization techniques among threads" /> <meta name="abstract" content="When you create code that is threadsafe but still benefits from sharing data or resources between threads, the most important aspect of programming becomes the ability to synchronize threads." /> <meta name="description" content="When you create code that is threadsafe but still benefits from sharing data or resources between threads, the most important aspect of programming becomes the ability to synchronize threads." /> <meta name="DC.Relation" scheme="URI" content="rzahwmulco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwmutco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwsemco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwvarco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwthrco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwsllco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwobjco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwrzahwcascasco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwsusco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwnonco.htm" /> <meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" /> <meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" /> <meta name="DC.Format" content="XHTML" /> <meta name="DC.Identifier" content="rzahwsyn-synco" /> <meta name="DC.Language" content="en-us" /> <!-- All rights reserved. Licensed Materials Property of IBM --> <!-- US Government Users Restricted Rights --> <!-- Use, duplication or disclosure restricted by --> <!-- GSA ADP Schedule Contract with IBM Corp. --> <link rel="stylesheet" type="text/css" href="./ibmdita.css" /> <link rel="stylesheet" type="text/css" href="./ic.css" /> <title>Synchronization techniques among threads</title> </head> <body id="rzahwsyn-synco"><a name="rzahwsyn-synco"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Synchronization techniques among threads</h1> <div><p>When you create code that is threadsafe but still benefits from sharing data or resources between threads, the most important aspect of programming becomes the ability to synchronize threads.</p> <p>Synchronization is the cooperative act of two or more threads that ensures that each thread reaches a known point of operation in relationship to other threads before continuing. Attempting to share resources without correctly using synchronization is the most common cause of damage to application data.</p> <p>Typically, synchronizing two threads involves the use of one or more synchronization primitives. Synchronization primitives are low-level functions or application objects (not <span class="keyword">i5/OS™</span> objects) that your application used or created to provide the synchronization behavior the application requires. </p> <div class="p">The most common synchronization primitives are as follows, in order of least to most computationally expensive: <ul><li>Compare and swap</li> <li>Mutual exclusion (mutexes) and threads</li> <li>Semaphores and threads</li> <li>Condition variables and threads</li> <li>Threads as synchronization primitives</li> <li>Space location locks</li> <li>Object locks</li> </ul> </div> <p>These concepts pertain to all programming languages. To determine how each language enables these concepts, refer to the <a href="../rzahg/rzahglanguages.htm">Language</a> topic in the information center for the specific language.</p> </div> <div> <ul class="ullinks"> <li class="ulchildlink"><strong><a href="rzahwmutco.htm">Mutexes and threads</a></strong><br /> A mutual exclusion (mutex) is used cooperatively between threads to ensure that only one of the cooperating threads is allowed to access the data or run certain application code at a time.</li> <li class="ulchildlink"><strong><a href="rzahwsemco.htm">Semaphores and threads</a></strong><br /> Semaphores (sometimes referred to as counting semaphores) can be used to control access to shared resources. A semaphore can be thought of as an intelligent counter. Every semaphore has a current count, which is greater than or equal to 0.</li> <li class="ulchildlink"><strong><a href="rzahwvarco.htm">Condition variables and threads</a></strong><br /> Condition variables allow threads to wait for certain events or conditions to occur and they notify other threads that are also waiting for the same events or conditions.</li> <li class="ulchildlink"><strong><a href="rzahwthrco.htm">Threads as synchronization primitives</a></strong><br /> Threads themselves can be used as synchronization primitives when one thread specifically waits for another thread to complete.</li> <li class="ulchildlink"><strong><a href="rzahwsllco.htm">Space location locks</a></strong><br /> A space location lock puts a logical lock on any single byte of storage. The lock does not change the storage or effect your application's access to the storage. The lock is a piece of information recorded by the system.</li> <li class="ulchildlink"><strong><a href="rzahwobjco.htm">Object locks</a></strong><br /> Object locks provide ways to acquire locks on specific system or application objects.</li> <li class="ulchildlink"><strong><a href="rzahwrzahwcascasco.htm">Compare and Swap</a></strong><br /> You can use the Machine Interface's (MI) Compare and Swap (CMPSWP) instruction to access data in a multithreaded program.</li> </ul> <div class="familylinks"> <div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwmulco.htm" title="If you are going to write multithreaded applications or server applications for i5/OS, you should be familiar with several techniques.">Multithreaded programming techniques</a></div> </div> <div class="relconcepts"><strong>Related concepts</strong><br /> <div><a href="rzahwsusco.htm" title="It is sometimes useful to stop a thread temporarily from processing in your application. When you suspend a thread, the state of the thread, including all the attributes and locks held by the thread, is maintained until that thread is resumed.">Suspend a thread</a></div> <div><a href="rzahwnonco.htm" title="Your multithreaded application at times requires access to functions or system services that are not thread safe. There are few completely safe alternatives for calling these functions.">Function calls that are not thread safe</a></div> </div> </div> </body> </html>