ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahw_5.4.0.1/rzahwsemco.htm

74 lines
4.7 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<?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="Semaphores and threads" />
<meta name="abstract" content="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." />
<meta name="description" content="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." />
<meta name="DC.Relation" scheme="URI" content="rzahwsynco.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahwe19rx.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="rzahwsem-semco" />
<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>Semaphores and threads</title>
</head>
<body id="rzahwsem-semco"><a name="rzahwsem-semco"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Semaphores and threads</h1>
<div><p>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. </p>
<p>Any thread can decrement the count to lock the semaphore (this is also
called waiting on the semaphore). Attempting to decrement the count past 0
causes the thread that is calling to wait for another thread to unlock the
semaphore. </p>
<p>Any thread can increment the count to unlock the semaphore (this is also
called posting the semaphore). Posting a semaphore might wake up a waiting
thread if there is one present. </p>
<p>In their simplest form (with an initial count of 1), semaphores can be
thought of as a mutual exclusion (mutex). The important distinction between
semaphores and mutexes is the concept of ownership. No ownership is associated
with a semaphore. Unlike mutexes, it is possible for a thread that never waited
for (locked) the semaphore to post (unlock) the semaphore. This can cause
unpredictable application behavior. You should avoid this if possible.</p>
<div class="p">Additional capabilities of some semaphore APIs provided
by the operating system are as follows: <ul><li>More complete management capabilities, including permissions on semaphores
that are similar to file permissions.</li>
<li>The ability to group semaphores in sets and perform atomic operations
on the group</li>
<li>The ability to do multicount wait and post operations</li>
<li>The ability to wait for a semaphore to have a count of 0</li>
<li>The ability to undo operations that are done by another thread under certain
conditions</li>
</ul>
</div>
<div class="p"> <div class="note"><span class="notetitle">Note:</span> Java™ does not have the ability to use semaphores.</div>
</div>
</div>
<div>
<ul class="ullinks">
<li class="ulchildlink"><strong><a href="rzahwe19rx.htm">Example: Use semaphores in Pthread programs to protect shared data</a></strong><br />
This example shows a Pthread program starting several threads that protect access to shared data with a semaphore set.</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>