ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/users_62.htm

347 lines
11 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>pthread_mutex_lock()--Lock Mutexe</title>
<!-- 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. -->
<!-- Begin Header Records ========================================== -->
<!-- NETMG2 SCRIPT A converted by B2H R4.1 (346) (CMS) by HOLTJM at -->
<!-- RCHVMW2 on 29 Jan 1999 at 10:01:37 -->
<!--File Edited November 2001 -->
<!--End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!-- Java sync-link -->
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
</script>
<a name="Top_Of_Page"></a>
<h2>pthread_mutex_lock()--Lock Mutex</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax:
<pre>
#include &lt;pthread.h&gt;
int pthread_mutex_lock(pthread_mutex_t *mutex);
</pre>
&nbsp;&nbsp;Service Program Name: QP0WPTHR<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE <br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Signal Safe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pthread_mutex_lock</strong>() function acquires ownership of the
mutex specified. If the mutex currently is locked by another thread, the call
to <strong>pthread_mutex_lock</strong>() blocks until that thread relinquishes
ownership by a call to <strong>pthread_mutex_unlock</strong>().</p>
<p>If a signal is delivered to a thread while that thread is waiting for a
mutex, when the signal handler returns, the wait resumes. <strong>
pthread_mutex_lock</strong>() does not return <strong>EINTR</strong> like some
other blocking function calls.</p>
<p>Use the CL command WRKJOB, option 20, to help you debug mutex deadlocks.</p>
<p>Destroying a held mutex is a common way to serialize destruction of objects
that are protected by that mutex. This action is allowed. The call to <strong>
pthread_mutex_lock</strong>() may fail with the <strong>EDESTROYED</strong>
error if the mutex is destroyed by the thread that was currently holding
it.</p>
<p>Mutex initialization using the <strong>PTHREAD_MUTEX_INITIALIZER</strong>
does not immediately initialize the mutex. Instead, on first use, <strong>
pthread_mutex_timedlock_np</strong>() or <strong>pthread_mutex_lock</strong>()
or <strong>pthread_mutex_trylock</strong>() branches into a slow path and
causes the initialization of the mutex. Because a mutex is not just a simple
memory object and requires that some resources be allocated by the system, an
attempt to call <strong>pthread_mutex_destroy</strong>() or <strong>
pthread_mutex_unlock</strong>() on a mutex that was statically initialized
using <strong>PTHREAD_MUTEX_INITIALIZER</strong> and was not yet locked causes an
<strong>EINVAL</strong> error.</p>
<p>A pthread mutex is a structure of type pthread_mutex_t that implement a
behavior based on the Pthread mutexes. An MI mutex is a structure built into
the machine that implement a similar sort of serialization construct.</p>
<p>The maximum number of recursive locks by the owning thread is 32,767. When
this number is exceeded, attempts to lock the mutex return the <strong>
ERECURSE</strong> error.</p>
<br>
<h3>Mutex Types</h3>
<p>A normal mutex cannot be locked repeatedly by the owner. Attempts by a
thread to relock an already held mutex, or to lock a mutex that was held by
another thread when that thread terminated, result in a deadlock condition.</p>
<p>A recursive mutex can be locked repeatedly by the owner. The mutex does not
become unlocked until the owner has called <strong>
pthread_mutex_unlock</strong>() for each successful lock request that it has
outstanding on the mutex.</p>
<p>An errorcheck mutex checks for deadlock conditions that occur when a thread
relocks an already held mutex. If a thread attempts to relock a mutex that it
already holds, the lock request fails with the <strong>EDEADLK</strong>
error.</p>
<p>An ownerterm mutex is an i5/OS extension to the errorcheck mutex type. An
ownerterm mutex checks for deadlock conditions that occur when a thread relocks
an already held mutex. If a thread attempts to relock a mutex that it already
holds, the lock request fails with the <strong>EDEADLK</strong> error. An
ownerterm mutex also checks for deadlock conditions that occur when a thread
attempts to lock a mutex that was held by another thread when that thread
terminated (an orphaned mutex). If a thread attempts to lock an orphaned mutex,
the lock request fails with the <strong>EOWNERTERM</strong> error.</p>
<p>When a thread terminates while holding a mutex lock on a normal or
errorcheck mutex, other threads that wait for that mutex will block forever.
The pthreads run-time simulates the deadlock that has occurred in your
application. When you are attempting to debug these deadlock scenarios, the CL
command WRKJOB, option 20 shows the thread as in a condition wait. Displaying
the call stack shows that the function
<strong>deadlockOnOrphanedMutex</strong> is in the call stack.</p>
<p>When a thread attempts to acquire a normal mutex that it already holds, the
thread will block forever. The pthreads run-time simulates the deadlock that
has occurred in your application. When you are attempting to debug these
deadlock scenarios, the CL command WRKJOB, option 20, shows the thread as in a
condition wait. Displaying the call stack will show that the function <strong>
deadlockOnAlreadyHeldMutex</strong> is in the call stack.</p>
<p>To change these behaviors, use an errorcheck or ownerterm mutex type.</p>
<br>
<h3>Authorities and Locks</h3>
<p>None.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong>mutex</strong></dt>
<dd>(Input) The address of the mutex to lock</dd>
</dl>
<br>
<h3>Return Value</h3>
<dl>
<dt><strong>0</strong></dt>
<dd><strong>pthread_mutex_lock</strong>() was successful.</dd>
<dt><strong>value</strong></dt>
<dd><strong>pthread_mutex_lock</strong>() was not successful. <em>value</em> is
set to indicate the error condition.</dd>
</dl>
<br>
<h3>Error Conditions</h3>
<p>If <strong>pthread_mutex_lock</strong>() was not successful, the error
condition returned usually indicates one of the following errors. Under some
conditions, the value returned could indicate an error other than those listed
here.</p>
<dl>
<dt><em>[EINVAL]</em></dt>
<dd><p>The value specified for the argument is not correct.</p></dd>
<dt><em>[EDESTROYED]</em></dt>
<dd><p>While waiting for the mutex lock to be satisfied, the mutex was
destroyed.</p></dd>
<dt><em>[EOWNERTERM]</em></dt>
<dd><p>A thread terminated the holding of the mutex, and the mutex is an ownerterm
mutex type.</p></dd>
<dt><em>[EDEADLK]</em></dt>
<dd><p></p>A thread attempted to relock an already held mutex, and the mutex is an
errorcheck mutex type.&nbsp;<a name="336837"></a><p></p></dd>
<dt><em>[ERECURSE]</em></dt>
<dd><p>The recursive mutex cannot be recursively locked again.</p></dd>
</dl>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>pthread.h</strong>&gt; header file. See <a href=
"rzah4hed.htm">Header files for Pthread functions</a>.<br><br></li>
<li><a href="users_60.htm">pthread_mutex_destroy()</a>--Destroy Mutex<br><br></li>
<li><a href="users_61.htm">pthread_mutex_init()</a>--Initialize Mutex<br><br></li>
<li><a href="users_64.htm">pthread_mutex_trylock()</a>--Lock Mutex with No
Wait<br><br></li>
<li><a href="users_63.htm">pthread_mutex_timedlock_np()</a>--Lock Mutex with Time-Out<br><br></li>
<li><a href="users_65.htm">pthread_mutex_unlock()</a>--Unlock Mutex</li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<pre>
#include &lt;pthread.h&gt;
#include &lt;stdio.h&gt;
#include "check.h"
/*
This example shows the corruption that can result if no
serialization is done and also shows the use of
pthread_mutex_lock(). Call it with no parameters
to use pthread_mutex_lock() to protect the critical section,
or 1 or more parameters to show data corruption that occurs
without locking.
*/
#define LOOPCONSTANT 100000
#define THREADS 10
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int i,j,k,l;
int uselock=1;
void *threadfunc(void *parm)
{
int loop = 0;
int rc;
for (loop=0; loop&lt;LOOPCONSTANT; ++loop) {
if (uselock) {
rc = pthread_mutex_lock(&amp;mutex);
checkResults("pthread_mutex_lock()\n", rc);
}
++i; ++j; ++k; ++l;
if (uselock) {
rc = pthread_mutex_unlock(&amp;mutex);
checkResults("pthread_mutex_unlock()\n", rc);
}
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t threadid[THREADS];
int rc=0;
int loop=0;
pthread_attr_t pta;
printf("Entering testcase\n");
printf("Give any number of parameters to show data corruption\n");
if (argc != 1) {
printf("A parameter was specified, no serialization is being done!\n");
uselock = 0;
}
pthread_attr_init(&amp;pta);
pthread_attr_setdetachstate(&amp;pta, PTHREAD_CREATE_JOINABLE);
printf("Creating %d threads\n", THREADS);
for (loop=0; loop&lt;THREADS; ++loop) {
rc = pthread_create(&amp;threadid[loop], &amp;pta, threadfunc, NULL);
checkResults("pthread_create()\n", rc);
}
printf("Wait for results\n");
for (loop=0; loop&lt;THREADS; ++loop) {
rc = pthread_join(threadid[loop], NULL);
checkResults("pthread_join()\n", rc);
}
printf("Cleanup and show results\n");
pthread_attr_destroy(&amp;pta);
pthread_mutex_destroy(&amp;mutex);
printf("\nUsing %d threads and LOOPCONSTANT = %d\n",
THREADS, LOOPCONSTANT);
printf("Values are: (should be %d)\n", THREADS * LOOPCONSTANT);
printf(" ==&gt;%d, %d, %d, %d\n", i, j, k, l);
printf("Main completed\n");
return 0;
}
</pre>
<p><strong>Output:</strong></p>
<pre>
Entering testcase
Give any number of parameters to show data corruption
Creating 10 threads
Wait for results
Cleanup and show results
Using 10 threads and LOOPCONSTANT = 100000
Values are: (should be 1000000)
==&gt;1000000, 1000000, 1000000, 1000000
Main completed
</pre>
<br>
<p><strong>Output:</strong></p>
<a name="336938"></a>
<h3>(data corruption without locking example)</h3>
<pre>
Entering testcase
Give any number of parameters to show data corruption
A parameter was specified, no serialization is being done!
Creating 10 threads
Wait for results
Cleanup and show results
Using 10 threads and LOOPCONSTANT = 100000
Values are: (should be 1000000)
==&gt;883380, 834630, 725131, 931883
Main completed
</pre>
<hr>
API introduced: V4R3
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"rzah4mst.htm">Pthread APIs</a> | <a href="aplist.htm">APIs by
category</a></td>
</tr>
</table>
</center>
</body>
</html>