2000-04-22 04:33:34 +08:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
|
2001-09-18 07:24:40 +08:00
|
|
|
<html>
|
|
|
|
<head>
|
2001-10-10 04:18:14 +08:00
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
|
|
<meta name="AUTHOR" content="pme@gcc.gnu.org (Phil Edwards)">
|
|
|
|
<meta name="KEYWORDS" content="HOWTO, libstdc++, gcc, g++, libg++, STL">
|
|
|
|
<meta name="DESCRIPTION" content="HOWTO for libstdc++ chapter 17.">
|
|
|
|
<meta name="GENERATOR" content="vi and eight fingers">
|
2001-09-18 07:24:40 +08:00
|
|
|
<title>libstdc++-v3 HOWTO: Chapter 17</title>
|
2001-10-12 02:41:47 +08:00
|
|
|
<link rel="StyleSheet" href="../lib3styles.css">
|
2001-09-18 07:24:40 +08:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2001-10-10 04:18:14 +08:00
|
|
|
<h1 class="centered"><a name="top">Chapter 17: Library Introduction</a></h1>
|
2001-09-18 07:24:40 +08:00
|
|
|
|
|
|
|
<p>Chapter 17 is actually a list of definitions and descriptions used
|
2000-04-22 04:33:34 +08:00
|
|
|
in the following chapters of the Standard when describing the actual
|
|
|
|
library. Here, we use "Introduction" as an introduction
|
2001-09-18 07:24:40 +08:00
|
|
|
to the <em>GNU implementation of</em> the ISO Standard C++ Library.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h1>Contents</h1>
|
|
|
|
<ul>
|
|
|
|
<li><a href="#2">The Standard C++ header files</a>
|
|
|
|
<li><a href="#3">Thread-safety</a>
|
|
|
|
<li><a href="#4"><code><foo></code> vs <code><foo.h></code></a>
|
2001-10-10 04:18:14 +08:00
|
|
|
<li><a href="porting-howto.html">Porting HOWTO</a>
|
2001-09-18 07:24:40 +08:00
|
|
|
</ul>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<h2><a name="2">The Standard C++ header files</a></h2>
|
|
|
|
<p>The Standard C++ Library specifies 50 header files that must be
|
2000-04-22 04:33:34 +08:00
|
|
|
available to all hosted implementations. Actually, the word
|
|
|
|
"files" is a misnomer, since the contents of the headers
|
|
|
|
don't necessarily have to be in any kind of external file. The
|
2001-09-18 07:24:40 +08:00
|
|
|
only rule is that when you <code>#include</code> a certain header, the
|
2000-04-22 04:33:34 +08:00
|
|
|
contents of that header, as defined by the Standard, become
|
|
|
|
available to you, no matter how.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The names of the headers can be easily seen in
|
|
|
|
<a href="headers_cc.txt"><code>testsuite/17_intro/headers.cc</code></a>,
|
2000-04-22 04:33:34 +08:00
|
|
|
which is a small testbed we use to make certain that the headers
|
|
|
|
all compile and run.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
|
|
|
<h2><a name="3">Thread-safety</a></h2>
|
|
|
|
<p>This is a thorny issue that gets brought up on the libstdc++-v3
|
2000-04-22 04:33:34 +08:00
|
|
|
and gcc mailing lists on a regular basis (probably by a cron job).
|
|
|
|
This entry will mention a very little bit about the general MT
|
|
|
|
issues with libstdc++. The latest status and quick notes will be
|
2001-05-30 16:30:04 +08:00
|
|
|
in FAQ 5.6. Some discussion about thread-safe containers will be
|
2001-06-08 11:53:35 +08:00
|
|
|
in section 6.8 (the HOWTOs on containers). This section only applies
|
|
|
|
when gcc and libstdc++-v3 were configured with --enable-threads.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>The libstdc++ code (all of it, not just the containers) has been
|
2000-04-22 04:33:34 +08:00
|
|
|
designed so that thread-safety will be easily possible. The first
|
2001-09-18 07:24:40 +08:00
|
|
|
(!) problem is finding a <em>fast</em> method of implementation
|
2000-04-22 04:33:34 +08:00
|
|
|
portable to all platforms. A minor problem that pops up every so
|
|
|
|
often is different interpretations of what "thread-safe"
|
|
|
|
means for a library (not a general program). We currently use the
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="http://www.sgi.com/tech/stl/thread_safety.html">same
|
|
|
|
definition that SGI</a> uses for their STL subset.
|
|
|
|
<em>Please see the many cautions given in HOWTOs on containers.</em>
|
|
|
|
</p>
|
|
|
|
<p>Here is another attempt at explaining the dangers of using the
|
2001-05-31 10:45:04 +08:00
|
|
|
STL with threading support without understanding some important
|
|
|
|
details. The STL implementation is currently configured to use
|
|
|
|
the high-speed caching memory allocator. If you absolutely
|
|
|
|
think you must change this on a global basis for your platform
|
|
|
|
to support multi-threading, then please consult all commentary
|
|
|
|
in include/bits/c++config and the HOWTOs on containers. Be
|
|
|
|
fully aware that you may change the external or internal ABI of
|
|
|
|
libstdc++-v3 when you provide -D__USE_MALLOC on the command line
|
2001-06-08 11:53:35 +08:00
|
|
|
or make a change to that configuration file.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>If you don't like caches of objects being retained inside the STL, then
|
2001-09-15 08:41:11 +08:00
|
|
|
you might be tempted to define __USE_MALLOC either on the command
|
|
|
|
line or by rebuilding c++config.h. Please note, once you define
|
|
|
|
__USE_MALLOC, only the malloc allocator is visible to application code
|
|
|
|
(i.e. the typically higher-speed allocator is not even available
|
|
|
|
in this configuration). There is a better way: It is possible
|
|
|
|
to force the malloc-based allocator on a per-case-basis for some
|
|
|
|
application code even when the above macro symbol is not defined.
|
|
|
|
The library team generally believes that this is a better way to tune
|
|
|
|
an application for high-speed using this implementation of the STL.
|
|
|
|
Here is one possible example displaying the forcing of the malloc-based
|
2001-05-31 10:45:04 +08:00
|
|
|
allocator over the typically higher-speed default allocator:
|
2001-10-10 04:18:14 +08:00
|
|
|
<pre>
|
2001-10-10 06:23:52 +08:00
|
|
|
std::list <my_type, std::malloc_alloc> my_malloc_based_list;</pre>
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>A recent journal article has described "atomic integer
|
2000-04-22 04:33:34 +08:00
|
|
|
operations," which would allow us to, well, perform updates
|
|
|
|
on integers atomically, and without requiring an explicit mutex
|
|
|
|
lock. This appears promising, but the major difficulty is that
|
|
|
|
these operations "may not be available on all systems, and
|
|
|
|
if they are, may have different interfaces." [quoting from
|
|
|
|
mailing list messages]
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Here is a small link farm to threads (no pun) in the mail archives
|
2000-04-22 04:33:34 +08:00
|
|
|
that discuss the threading problem. Each link is to the first
|
|
|
|
relevent message in the thread; from there you can use
|
|
|
|
"Thread Next" to move down the thread. This farm is in
|
|
|
|
latest-to-oldest order.
|
2001-09-18 07:24:40 +08:00
|
|
|
<ul>
|
|
|
|
<li><a href="http://gcc.gnu.org/ml/libstdc++/2001-05/msg00384.html">
|
2001-09-15 08:41:11 +08:00
|
|
|
inspired this most recent updating of issues with threading
|
|
|
|
and the SGI STL library. It also contains some example
|
2001-09-18 07:24:40 +08:00
|
|
|
POSIX-multithreaded STL code.</a>
|
|
|
|
<li> <a href="http://gcc.gnu.org/ml/libstdc++/2001-05/msg00136.html">
|
2001-09-15 08:41:11 +08:00
|
|
|
an early analysis of why __USE_MALLOC should be disabled for
|
2001-09-18 07:24:40 +08:00
|
|
|
the 3.0 release of libstdc++.</a>
|
|
|
|
</ul>
|
|
|
|
<br>
|
2000-04-22 04:33:34 +08:00
|
|
|
Here are discussions that took place before the current snapshot;
|
2001-09-15 08:41:11 +08:00
|
|
|
they are still relevant and instructive. (Some of them may not work;
|
|
|
|
as the drive containing some of the 1999 archives crashed, and nobody
|
|
|
|
has had time to recover the backups.)
|
2001-09-18 07:24:40 +08:00
|
|
|
<br>
|
|
|
|
<ul>
|
|
|
|
<li>One way of preventing memory leaks by the old default memory
|
2000-04-22 04:33:34 +08:00
|
|
|
allocator in multithreaded code is
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="http://gcc.gnu.org/ml/gcc/1999-11n/msg00431.html">discussed here</a>.
|
|
|
|
<li><a href="http://gcc.gnu.org/ml/libstdc++/1999-q3/msg00167.html">This thread
|
|
|
|
concerns strings</a>.
|
|
|
|
<li><a href="http://gcc.gnu.org/ml/libstdc++/1999-q2/msg00339.html">So does this
|
|
|
|
one</a>. This initial message also refers to another
|
2000-04-22 04:33:34 +08:00
|
|
|
thread in the GCC mailing list...
|
2001-09-18 07:24:40 +08:00
|
|
|
<li><a href="http://gcc.gnu.org/ml/gcc/1999-06n/msg00680.html">which is here</a>,
|
2000-04-22 04:33:34 +08:00
|
|
|
and goes on for some time. Ironically, the initial message
|
|
|
|
in this thread also mentions another threading thread...
|
2001-09-18 07:24:40 +08:00
|
|
|
<li><a href="http://gcc.gnu.org/ml/gcc-bugs/1999-04n/msg00777.html">beginning here</a>,
|
2000-04-22 04:33:34 +08:00
|
|
|
and talking about pthreads. (Note that a much more recent
|
|
|
|
message from the first thread in this list notes that
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="http://gcc.gnu.org/ml/libstdc++/1999-q3/msg00176.html">pthreads
|
|
|
|
should not be used as a starting point</a> for making
|
2000-04-22 04:33:34 +08:00
|
|
|
libstdc++ threadsafe.)
|
2001-09-18 07:24:40 +08:00
|
|
|
<li><a href="http://gcc.gnu.org/ml/libstdc++/1999-q2/msg00168.html">This
|
|
|
|
message</a>,
|
|
|
|
<a href="http://gcc.gnu.org/ml/libstdc++/1999-q2/msg00159.html">this one</a>,
|
|
|
|
and <a href="http://gcc.gnu.org/ml/libstdc++/1999-q2/msg00156.html">this one</a>
|
2000-04-22 04:33:34 +08:00
|
|
|
are the tops of related threads (all within the same time
|
|
|
|
period) discussing threading and the IO library. Much of it
|
|
|
|
is dealing with the C library, but C++ is included as well.
|
2001-09-18 07:24:40 +08:00
|
|
|
</ul>
|
|
|
|
</p>
|
|
|
|
<p>This section will be updated as new and interesting issues come
|
2000-04-22 04:33:34 +08:00
|
|
|
to light.
|
2001-09-18 07:24:40 +08:00
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
<h2><a name="4"><code><foo></code> vs <code><foo.h></code></a></h2>
|
|
|
|
<p>The new-style headers are fully supported in libstdc++-v3. The compiler
|
|
|
|
itself fully supports namespaces, including <code>std::</code>.
|
|
|
|
</p>
|
|
|
|
<p>For those of you new to ISO C++98, no, that isn't a typo, the headers
|
2000-04-22 04:33:34 +08:00
|
|
|
really have new names. Marshall Cline's C++ FAQ Lite has a good
|
|
|
|
explanation in
|
2001-09-18 07:24:40 +08:00
|
|
|
<a href="http://www.cerfnet.com/~mpcline/On-Line-C++-FAQ/coding-standards.html#[25.4]">item [25.4]</a>.
|
|
|
|
</p>
|
|
|
|
<p>Return <a href="#top">to top of page</a> or
|
|
|
|
<a href="../faq/index.html">to the FAQ</a>.
|
|
|
|
</p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- ####################################################### -->
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
<hr>
|
2001-10-10 04:18:14 +08:00
|
|
|
<p class="fineprint"><em>
|
2001-10-05 04:03:22 +08:00
|
|
|
See <a href="license.html">license.html</a> for copying conditions.
|
2000-04-22 04:33:34 +08:00
|
|
|
Comments and suggestions are welcome, and may be sent to
|
2001-10-10 04:18:14 +08:00
|
|
|
<a href="mailto:libstdc++@gcc.gnu.org">the libstdc++ mailing list</a>.
|
2001-09-18 07:24:40 +08:00
|
|
|
</em></p>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|
2001-09-18 07:24:40 +08:00
|
|
|
</body>
|
|
|
|
</html>
|
2000-04-22 04:33:34 +08:00
|
|
|
|
|
|
|
|