Symbian3/SDK/Source/GUID-FA120B3F-4EC4-5A0A-8A36-D5CD032CF1B0.dita
author Dominic Pinkman <Dominic.Pinkman@Nokia.com>
Thu, 21 Jan 2010 18:18:20 +0000
changeset 0 89d6a7a84779
permissions -rw-r--r--
Initial contribution of Documentation_content according to Feature bug 1266 bug 1268 bug 1269 bug 1270 bug 1372 bug 1374 bug 1375 bug 1379 bug 1380 bug 1381 bug 1382 bug 1383 bug 1385

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept xml:lang="en" id="GUID-FA120B3F-4EC4-5A0A-8A36-D5CD032CF1B0"><title>Using Mutexes</title><prolog><metadata><keywords/></metadata></prolog><conbody><p>Mutexes provide serialised access to shared resources. They are Kernel objects and, as such, are managed by the Kernel.</p> <p>A mutex can be used by threads across any number of processes. If a resource is only shared between the threads within the same process, it can be more efficient to use a critical section.</p> <p>Access to a mutex is through an RMutex handle.</p> <p>Mutexes are similar to semaphores in that they have a <codeph>TInt</codeph> count value that is incremented by calling the <codeph>Signal()</codeph> member function of the mutex handle and decremented by calling the <codeph>Wait()</codeph> member function of the mutex handle. A mutex with a negative value implies that a thread must wait for access to the shared resource.</p> <p>Unlike a semaphore, the count value of a mutex is always set to one when it is constructed.</p> <p>The creator of a shared resource uses the <codeph>CreateLocal()</codeph> or <codeph>CreateGlobal()</codeph> member functions of <codeph>RMutex</codeph> to create a mutex and to open the handle to it. Any thread wishing to access the resource first calls <codeph>Wait()</codeph>; that thread then calls <codeph>Signal()</codeph> after completing its access.</p> <p>The first thread to call <codeph>Wait()</codeph> returns immediately, and is free to continue execution, but any other threads that call <codeph>Wait()</codeph> will wait in a queue maintained by the mutex. Waiting threads are released on a first-in first-out basis when the thread currently accessing the resource calls <codeph>Signal()</codeph>.</p> <p>The nature of the shared resources should be such that any access completes in a relatively short time so that threads do not wait for extensive periods on the mutex.</p> <p>In many cases, it may be better to use servers to serialize access to a shared resource (for example, the window server) rather than use a mutex. </p> </conbody></concept>