<p><br> </p>
<p><br> </p>
<p><br> </p>
<p></p>
<ul><li><span style="font-size:24px">Quote </span>
<ul><li>From : <span style="font-size:12px">http://ju.outofmemory.cn/entry/66238 </span></li></ul> </li></ul>
<p></p>
<p></p>
<p style="line-height:1.6em; margin-top:1em; margin-bottom:1em; font-family:"Helvetica Neue",Helvetica,STHeiTi,"Microsoft YaHei"; font-size:16.96px"> 我查阅了相关的资料, 发现线程局部存储(TLS)是一个后来者, 产生于多线程概念之后.而在软件发展的早期, 全局变量经常用在库函数中, 用于存储全局信息, 比如<code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,serif; background-color:rgb(247,247,247)"><span class="pln">errno</span></code>, 多线程程序产生之后, 全局变量<code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,serif; background-color:rgb(247,247,247)"><span class="pln">errno</span></code>就成为所有线程都共享的一个变量, 而实际上, 每个线程都想维护一份自己的<code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,serif; background-color:rgb(247,247,247)"><span class="pln">errno</span></code>, 隔离于其他线程.</p>
<p style="line-height:1.6em; margin-top:1em; margin-bottom:1em; font-family:"Helvetica Neue",Helvetica,STHeiTi,"Microsoft YaHei"; font-size:16.96px"> 这个时候, 没人愿意去修改库函数的接口. 于是线程局部存储就诞生了, 根据<a href="http://en.wikipedia.org/wiki/Thread-local_storage" rel="noopener noreferrer" style="" target="_blank">wikipedia</a>的介绍</p>
<blockquote style="margin:0.8em 0.5em; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(240,240,240) rgb(240,240,240) rgb(240,240,240) rgb(102,153,204); padding:2px 2px 4px 5px; font-family:"Helvetica Neue",Helvetica,STHeiTi,"Microsoft YaHei"; font-size:16.96px">
<p style="line-height:1.6em; margin-top:1em; margin-bottom:1em">Thread-local storage (TLS) is a computer programming method that uses static or global memory local to a thread.</p>
</blockquote>
<p style="line-height:1.6em; margin-top:1em; margin-bottom:1em; font-family:"Helvetica Neue",Helvetica,STHeiTi,"Microsoft YaHei"; font-size:16.96px"> 为了在各个平台上都能用上线程局部变量, <code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,serif; background-color:rgb(247,247,247)"><span class="pln">POSIX </span><span class="typ" style="color:rgb(43,145,175)">Thread</span></code>定义了一组接口, 用于显式构造使用线程局部存储.</p>
<pre class="prettyprint prettyprinted" style="padding:3px; font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,serif; margin-bottom:10px; background-color:rgb(225,225,225); overflow:auto; width:auto; font-size:0.8em; line-height:1.5em"><code class="blockcode" style="font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",monospace,serif; background-color:rgb(247,247,247); max-height:600px; display:block; overflow:auto"><span class="com" style="color:gray">#include</span><span class="pln"> </span><span class="str" style="color:rgb(128,0,0)"><pthread.h></span><span class="pln">
</span><span class="kwd" style="color:rgb(0,0,139)">int</span><span class="pln"> pthread_key_create</span><span class="pun">(</span><span class="typ" style="color:rgb(43,145,175)">pthread_key_t</span><span class="pln"> </span><span class="pun">*</span><span class="pln">key</span><span class="pun">,</span><span class="pln"> </span><span class="kwd" style="color:rgb(0,0,139)">void</span><span class="pln"> </span><span class="pun">(*</span><span class="pln">destructor</span><span class="pun">)(</span><span class="kwd" style="color:rgb(0,0,139)">void</span><span class="pun">*));</span><span class="pln">
</span><span class="kwd" style="color:rgb(0,0,139)">int</span><span class="pln"> pthread_key_delete</span><span class="pun">(</span><span class="typ" style="color:rgb(43,145,175)">pthread_key_t</span><span class="pln"> key</span><span class="pun">);</span><span class="pln">
</span><span class="kwd" style="color:rgb(0,0,139)">void</span><span class="pln"> </span><span class="pun">*</span><span class="pln">pthread_getspecific</span><span class="pun">(</span><span class="typ" style="color:rgb(43,145,175)">pthread_key_t</span><span class="pln"> key</span><span class="pun">);</span><span class="pln">
</span><span class="kwd" style="color:rgb(0,0,139)">int</span><span class="pln"> pthread_setspecific</span><span class="pun">(</span><span class="typ" style="color:rgb(43,145,175)">pthread_key_t</span><span clas |
|