<?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="reference" /> <meta name="DC.Title" content="Mutexes and threads" /> <meta name="abstract" content="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." /> <meta name="description" content="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." /> <meta name="DC.Relation" scheme="URI" content="rzahwsynco.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwe18rx.htm" /> <meta name="DC.Relation" scheme="URI" content="rzahwex4rx.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="rzahwmut-mutco" /> <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>Mutexes and threads</title> </head> <body id="rzahwmut-mutco"><a name="rzahwmut-mutco"><!-- --></a> <!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script> <h1 class="topictitle1">Mutexes and threads</h1> <div><p>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.</p> <div class="section"><p>The word mutex is shorthand for a primitive object that provides MUTual EXclusion between threads. For the purposes of this introduction, you can think of mutexes as similar to critical sections and monitors.</p> </div> <div class="section"><p>The mutex is typically logically associated with the data it protects by the application. For example, PAYROLL DATA has a PAYROLL MUTEX associated with it. The application code always locks the PAYROLL MUTEX before accessing the PAYROLL DATA. The mutex prevents access to the data by a thread only if that thread uses the mutex before accessing the data.</p> </div> <div class="section"><p>Create, lock, unlock, and destroy are operations typically performed on a mutex. Any thread that successfully locks the mutex is the owner until it unlocks the mutex. Any thread that attempts to lock the mutex waits until the owner unlocks the mutex. When the owner unlocks the mutex, control is returned to one waiting thread with that thread becoming the owner of the mutex. There can be only one owner of a mutex.</p> </div> <div class="section"><p>Mutex wait operations can be recursive. Recursive mutexes allow the owner to lock the mutex repeatedly. The owner of the mutex remains the same until the number of unlock requests is the same as the number of successful lock requests. Mutex waits can time out after a user specified amount of time or can return immediately if they cannot acquire the lock. For more information, see your API set documentation about mutex primitives available for your application.</p> </div> </div> <div> <ul class="ullinks"> <li class="ulchildlink"><strong><a href="rzahwe18rx.htm">Example: Use mutexes in Pthread programs</a></strong><br /> This example shows a Pthread program starting several threads that protect access to shared data with a mutual exclusion (mutex).</li> <li class="ulchildlink"><strong><a href="rzahwex4rx.htm">Example: Use mutexes in Java</a></strong><br /> This example shows a Java™ program creating a critical section of code. In Java™, any object or array can function similarly to a mutual exclusion (mutex) using the synchronized keyword on a block of code or a method.</li> </ul> <div class="familylinks"> <div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwsynco.htm" title="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.">Synchronization techniques among threads</a></div> </div> </div> </body> </html>