thread_safety.html.en revision ac082aefa89416cbdc9a1836eaf3bed9698201c8
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><!--
e05c80bc62969c6c8b9b2d5b7fd573a8f24b68a3fanf XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf This file is generated from xml source: DO NOT EDIT
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
c54a1ccf3f6fcf4a4c280e4dfae88d45a6f4aef2dreid<title>Apache HTTP Server 2.x Thread Safety Issues - Apache HTTP Server</title>
c54a1ccf3f6fcf4a4c280e4dfae88d45a6f4aef2dreid<link href="/style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
c54a1ccf3f6fcf4a4c280e4dfae88d45a6f4aef2dreid<link href="/style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
c54a1ccf3f6fcf4a4c280e4dfae88d45a6f4aef2dreid<link href="/style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="/style/css/prettify.css" />
c54a1ccf3f6fcf4a4c280e4dfae88d45a6f4aef2dreid<script src="/style/scripts/prettify.js" type="text/javascript">
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<link href="/images/favicon.ico" rel="shortcut icon" /></head>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<div class="up"><a href="./"><img title="<-" alt="<-" src="/images/left.gif" /></a></div>
e05c80bc62969c6c8b9b2d5b7fd573a8f24b68a3fanf<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Developer Documentation</a></div><div id="page-content"><div id="preamble"><h1>Apache HTTP Server 2.x Thread Safety Issues</h1>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<p><span>Available Languages: </span><a href="/en/developer/thread_safety.html" title="English"> en </a></p>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf <p>When using any of the threaded mpms in the Apache HTTP Server 2.x it is important
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf that every function called from Apache be thread safe. When linking in 3rd
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf party extensions it can be difficult to determine whether the resulting
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf server will be thread safe. Casual testing generally won't tell you this
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf either as thread safety problems can lead to subtle race conditions that
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf may only show up in certain conditions under heavy load.</p>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<div id="quickview"><ul id="toc"><li><img alt="" src="/images/down.gif" /> <a href="#variables">Global and static variables</a></li>
0dd3c9e8adf97e277b7d611b976294c61abd8476slive<li><img alt="" src="/images/down.gif" /> <a href="#errno">errno</a></li>
0dd3c9e8adf97e277b7d611b976294c61abd8476slive<li><img alt="" src="/images/down.gif" /> <a href="#functions">Common standard troublesome functions</a></li>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<li><img alt="" src="/images/down.gif" /> <a href="#commonlibs">Common 3rd Party Libraries</a></li>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<li><img alt="" src="/images/down.gif" /> <a href="#liblist">Library List</a></li>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf</ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<div class="top"><a href="#page-header"><img alt="top" src="/images/up.gif" /></a></div>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf<h2><a name="variables" id="variables">Global and static variables</a></h2>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf <p>When writing your module or when trying to determine if a module or
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf 3rd party library is thread safe there are some common things to keep in
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf <p>First, you need to recognize that in a threaded model each individual
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf thread has its own program counter, stack and registers. Local variables
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf live on the stack, so those are fine. You need to watch out for any
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf static or global variables. This doesn't mean that you are absolutely not
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf allowed to use static or global variables. There are times when you
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf actually want something to affect all threads, but generally you need to
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf avoid using them if you want your code to be thread safe.</p>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf <p>In the case where you have a global variable that needs to be global and
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf accessed by all threads, be very careful when you update it. If, for
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf example, it is an incrementing counter, you need to atomically increment
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf it to avoid race conditions with other threads. You do this using a mutex
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf (mutual exclusion). Lock the mutex, read the current value, increment it
e05c80bc62969c6c8b9b2d5b7fd573a8f24b68a3fanf and write it back and then unlock the mutex. Any other thread that wants
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf to modify the value has to first check the mutex and block until it is
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf cleared.</p>
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf <p>If you are using <a href="http://apr.apache.org/">APR</a>, have a look
0c555d3c073eb871bce725f7b5cb9577a8382b7ffanf at the <code>apr_atomic_<var>*</var></code> functions and the
c54a1ccf3f6fcf4a4c280e4dfae88d45a6f4aef2dreid <code>apr_thread_mutex_<var>*</var></code> functions.</p>
the tokenizing state. If you are using <a href="http://apr.apache.org/">APR</a> you can use <code>apr_strtok()</code>.</p>
<p>Please drop a note to <a href="http://httpd.apache.org/lists.html#http-dev">dev@httpd.apache.org</a>
<table class="bordered"><tr class="header"><th>Library</th><th>Version</th><th>Thread Safe?</th><th>Notes</th></tr>
<td>ImageMagick docs claim it is thread safe since version 5.2.2 (see <a href="http://www.imagemagick.com/www/changelog.html">Change log</a>).
<tr class="odd"><td><a href="http://www.enlightenment.org/p.php?p=about/efl&l=en">Imlib2</a></td>
more information, please read <a href="http://dev.mysql.com/doc/mysql/en/Threaded_clients.html">http://dev.mysql.com/doc/mysql/en/Threaded_clients.html</a>.</td></tr>
has been partially thread-safe since V1.91: <a href="http://www.pdflib.com/products/pdflib-family/pdflib/">http://www.pdflib.com/products/pdflib-family/pdflib/</a>.</td></tr>
<tr><td><a href="http://www.postgresql.org/docs/8.4/static/libpq-threading.html">libpq (PostgreSQL)</a></td>
<p><span>Available Languages: </span><a href="/en/developer/thread_safety.html" title="English"> en </a></p>
</div><div class="top"><a href="#page-header"><img src="/images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
var comments_identifier = 'http://httpd.apache.org/docs/trunk/developer/thread_safety.html';
if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
d.write('<div id="comments_thread"><\/div>');
var s = d.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
(d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
<p class="apache">Copyright 2012 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
<p class="menu"><a href="/mod/">Modules</a> | <a href="/mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="/glossary.html">Glossary</a> | <a href="/sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--