87 lines
5.2 KiB
HTML
87 lines
5.2 KiB
HTML
|
<?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="Storage usage and threaded applications" />
|
||
|
<meta name="abstract" content="When writing a threaded application, it is important to understand the visibility and scope of various classes of storage." />
|
||
|
<meta name="description" content="When writing a threaded application, it is important to understand the visibility and scope of various classes of storage." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rzahwsafco.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="rzahwsto-stoco" />
|
||
|
<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>Storage usage and threaded applications</title>
|
||
|
</head>
|
||
|
<body id="rzahwsto-stoco"><a name="rzahwsto-stoco"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Storage usage and threaded applications</h1>
|
||
|
<div><p>When writing a threaded application, it is important to understand
|
||
|
the visibility and scope of various classes of storage.</p>
|
||
|
<div class="section"><div class="p">When your application declares variables, multiple threads might
|
||
|
be able to access or use them. The visibility and scope of the storage it
|
||
|
uses often affect your application.<ul><li>Global storage:<p>Global storage that you declare in one of your application
|
||
|
source files is visible to all other source files or modules in your application.
|
||
|
All threads that run code in your application share the same global storage.
|
||
|
This unintended sharing of storage is a common safety problem.</p>
|
||
|
</li>
|
||
|
<li>Static storage:<p>Static storage is global storage with visibility that
|
||
|
is restricted to the source file, module, or function in which the storage
|
||
|
or variable was declared. All threads that run code from that module share
|
||
|
the same static storage. </p>
|
||
|
</li>
|
||
|
<li>Heap storage:<p>Heap storage is allocated and deallocated dynamically
|
||
|
by your application (for example, by <span class="cmdname">malloc()</span> and <span class="cmdname">free()</span> in
|
||
|
C, or new and delete in Java™ or C++). All threads that run code
|
||
|
in your application share the same heap. </p>
|
||
|
<p>If you give another thread
|
||
|
a pointer to the heap storage that has been allocated (by storing the pointer
|
||
|
in static or global storage, or by otherwise passing the pointer to another
|
||
|
thread) that thread can use or deallocate the storage. This unintended sharing
|
||
|
of storage is a common thread safety problem. </p>
|
||
|
</li>
|
||
|
<li>Automatic storage:<p>Automatic or local storage is allocated automatically
|
||
|
by your language when you declare a variable that is private to a function,
|
||
|
method, or subroutine. Local storage is not visible to other threads in the
|
||
|
process. Each time those threads call the function, method, or subroutine,
|
||
|
the thread allocates new versions of the automatic variables. Each thread
|
||
|
gets its own automatic storage. A thread should not access automatic storage
|
||
|
from another thread because of the complex serialization and synchronization
|
||
|
issues involved.</p>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="section"><p>The operating system further restricts
|
||
|
the scope of global, static, and heap storage to the activation group that
|
||
|
your application code is running in. This means that application code or threads
|
||
|
running in different activation groups but using the same global or static
|
||
|
variables access different versions of those variables and their storage.
|
||
|
Similarly, heap storage allocated in one activation group might not be de-allocated
|
||
|
in a different activation group although it can be used by different activation
|
||
|
groups. </p>
|
||
|
</div>
|
||
|
<div class="section"><p>Some languages support only global or static storage classes for
|
||
|
variables. These languages are not easy to use in threaded applications. You
|
||
|
should not use threads in these languages.</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahwsafco.htm" title="A function is threadsafe if you can start it simultaneously in multiple threads within the same process. A function is threadsafe if and only if all the functions it calls are also threadsafe.">Thread safety</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|