⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_safety.html

📁 指导程序员合理、高效的进行标准模板库编程。
💻 HTML
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <TITLE>MFC Programmer's SourceBook : STL Programmer's Guide</TITLE>
    <META name="description" 
     content="A freely available implementation 
     of the C++ Standard Template Library, including 
     hypertext documentation.">
	<META name="keywords" 
	content="generic programming, STL, standard template library">
</HEAD>

<SCRIPT LANGUAGE="JavaScript"><!--
var adcategory = "cpp";
// -->
</SCRIPT>
<body background="../../fancyhome/back.gif" bgcolor="#FFFFFF" >
<SCRIPT LANGUAGE="JavaScript"><!--
var nfrm = location.href.indexOf("_nfrm_");
var validframes = (top.frames.length > 0 && top.frames['ad'] && top.frames['logo'] );
var random = Math.random();

if( !validframes && nfrm == -1 )
{
	var dclkPage = "www.codeguru.com/";
	if( self.adcategory )
		dclkPage += adcategory;
	else
		dclkPage += "mfc";
	document.write('<nolayer><center>');
	document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
	 + random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
	 + 'frameborder=0 scrolling=no bordercolor="#000000">');
	document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
	 + random + '">');
	document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
	 + random + '" height=60 width=468>' + '</a>');
	document.write('</iframe>');
	document.write('</center></nolayer>');
	document.write('<layer  src="http://ad.doubleclick.net/adl/' + dclkPage + 
	 ';ord=' + random + '"></layer>');
	document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}


//		top.location = "/show.cgi?" + adcategory + "=" + location.pathname;


// -->
</SCRIPT>
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupcCNFCY34AAHpDM10">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupcCNFCY34AAHpDM10"></a>
</p>
</noscript>






<H1>Thread-safety for SGI STL</H1>
<P>
SGI STL provides what 
we believe to be the most useful form of thread-safety. This explains 
some of the design decisions made in the SGI STL implementation. </P>

<H2>Client must lock shared mutable containers</H2>
<P>
The SGI implementation of STL is thread-safe only in the sense that 
simultaneous accesses to distinct containers are safe, and simultaneous 
read accesses to to shared containers are safe. If multiple threads 
access a single container, and at least one thread may potentially 
write, then the user is responsible for ensuring mutual exclusion 
between the threads during the container accesses. </P>
<P>
This is the only way to ensure full performance for containers that do 
not need concurrent access. Locking or other forms of synchronization 
are typically expensive and should be avoided when not necessary. </P>
<P>
It is easy for the client or another library to provide the necessary 
locking by wrapping the underlying container operations with a lock 
acquisition and release. For example, it would be possible to provide a <TT>locked_queue</TT>
 container adapter that provided a container with atomic queue 
operations. </P>
<P>
For most clients, it would be insufficient to simply make container 
operations atomic; larger grain atomic actions are needed. If a user's 
code needs to increment the third element in a vector of counters, it 
would be insuffcient to guarantee that fetching the third element and 
storing the third element is atomic; it is also necessary to guarantee 
that no other updates occur in the middle. Thus it would be useless for 
vector operations to acquire the lock; the user code must provide for 
locking in any case. </P>
<P>
This decision is different from that made by the Java designers. There 
are two reasons for that. First, for security reasons Java must 
guarantee that even in the presence of unprotected concurrent accesses 
to a container, the integrity of the virtual machine cannot be 
violated. Such safety constraints were clearly not a driving force 
behind either C++ or STL. Secondly, performance was a more important 
design goal for STL then it was for the Java standard library. </P>
<P>
On the other hand, this notion of thread-safety is stronger than that 
provided by reference-counted string implementations that try to follow 
the CD2 version of the draft standard. Such implementations require 
locking between multiple readers of a shared string. </P>

<H2>Lock implementation</H2>
<P>
The SGI STL implementation removes all nonconstant static data from 
container implementations. The only potentially shared static data 
resides in the allocator implementations. To this end, the code to 
implement per-class node allocation in HP STL was transformed into 
inlined code for per-size node allocation in the SGI STL allocators. 
Currently the only explicit locking is performed inside <A
 HREF="Allocators.html">allocators</A>. 
 </P>
<P>
Many other container implementations should also benefit from this 
design. It will usually be possible to implement thread-safe containers 
in portable code that does not depend on any particular thread package 
or locking primitives. </P>
<P>
Alloc.h uses three different locking primitives depending on the 
environment. In addition, it can be forced to perform no locking by 
defining <TT>_NOTHREADS</TT>. The three styles of locking are: </P>
<UL>
    <LI>
    Pthread mutexes. These are used if <TT>_PTHREADS</TT> is defined by 
    the user. This may be done on SGI machines, but is not recommended in 
    performance critical code with the currently (March 1997) released 
    versions of the SGI Pthreads libraries. 
    <LI>
    Win32 critical sections. These are used by default for win32 
    compilations with compiler options that request multi-threaded code. 
    <LI>
    An SGI specific spin-lock implementation that is usable with both 
    pthread and sproc threads. This could serve as a prototype 
    implementation for other platforms. This is the default on SGI/MIPS 
    platforms. 
</UL>
<P>
It would be preferable if we could always use the OS-supplied locking 
primitives. Unfortunately, these often do not perform well, for very 
short critical sections such as those used by the allocator. </P>
<P>
Allocation intensive applications using Pthreads to obtain concurrency 
on multiprocessors should consider using pthread_alloc from <A
 HREF="pthread_alloc.h">pthread_alloc.h</A>. 
It imposes the restriction that memory deallocated by a thread can only 
be reallocated by that thread. However, it often obtains significant 
performance advantages as a result. </P>

<HR SIZE="6"> <FONT SIZE="-2"> Copyright &copy; 1996 Silicon Graphics, Inc.
<HR>
<TABLE BORDER=0 WIDTH="100%" >
<TR>
<TD WIDTH="33%"><FONT SIZE=-1><A HREF="index.html" >
STL</A></FONT></TD>

<TD WIDTH="33%">
<CENTER><FONT SIZE=-2>&copy; Copyright 1997-1998 CodeGuru</FONT>&nbsp;</CENTER>
</TD>

<TD WIDTH="34%">
<DIV ALIGN=right><FONT SIZE=-1>Contact : <A HREF="mailto:webmaster@codeguru.com">webmaster@codeguru.com</A>&nbsp;</FONT></DIV>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="JavaScript" ><!--
var adurl = "/cgi-bin/doubleclick.cgi?";

if( self.adcategory )
	adurl += adcategory;
else
	adurl += "mfc";

if( self.parent.norefreshad )
	parent.norefreshad = false;
else if( validframes )
	parent.frames['ad'].location = adurl;



if( !validframes && nfrm == -1)
{
	var dclkPage = "www.codeguru.com/";
	if( self.adcategory )
		dclkPage += adcategory;
	else 
		dclkPage += "mfc";
//	var random = Math.random();
	document.write('<nolayer><center>');
	document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
	 + random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
	 + 'frameborder=0 scrolling=no bordercolor="#000000">');
	document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
	 + random + '">');
	document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
	 + random + '" height=60 width=468>' + '</a>');
	document.write('</iframe>');
	document.write('</center></nolayer>');
	document.write('<layer  src="http://ad.doubleclick.net/adl/' + dclkPage + 
	 ';ord=' + random + '"></layer>');
	document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}

// -->
</SCRIPT> 
<!-- SCRIPT LANGUAGE="JavaScript" SRC="/global/fscript.js">
//
</SCRIPT --> 

<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupcCNFCY34AAHpDM10">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupcCNFCY34AAHpDM10"></a>
</p>
</noscript>





</BODY>
</HTML>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -