C++ Thread Local Storage

论坛 期权论坛     
选择匿名的用户   2021-6-2 17:55   348   0
<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:&#34;Helvetica Neue&#34;,Helvetica,STHeiTi,&#34;Microsoft YaHei&#34;; font-size:16.96px"> 我查阅了相关的资料, 发现线程局部存储(TLS)是一个后来者, 产生于多线程概念之后.而在软件发展的早期, 全局变量经常用在库函数中, 用于存储全局信息, 比如<code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,&#34;Lucida Console&#34;,&#34;Liberation Mono&#34;,&#34;DejaVu Sans Mono&#34;,&#34;Bitstream Vera Sans Mono&#34;,&#34;Courier New&#34;,monospace,serif; background-color:rgb(247,247,247)"><span class="pln">errno</span></code>, 多线程程序产生之后, 全局变量<code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,&#34;Lucida Console&#34;,&#34;Liberation Mono&#34;,&#34;DejaVu Sans Mono&#34;,&#34;Bitstream Vera Sans Mono&#34;,&#34;Courier New&#34;,monospace,serif; background-color:rgb(247,247,247)"><span class="pln">errno</span></code>就成为所有线程都共享的一个变量, 而实际上, 每个线程都想维护一份自己的<code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,&#34;Lucida Console&#34;,&#34;Liberation Mono&#34;,&#34;DejaVu Sans Mono&#34;,&#34;Bitstream Vera Sans Mono&#34;,&#34;Courier New&#34;,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:&#34;Helvetica Neue&#34;,Helvetica,STHeiTi,&#34;Microsoft YaHei&#34;; 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:&#34;Helvetica Neue&#34;,Helvetica,STHeiTi,&#34;Microsoft YaHei&#34;; 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:&#34;Helvetica Neue&#34;,Helvetica,STHeiTi,&#34;Microsoft YaHei&#34;; font-size:16.96px"> 为了在各个平台上都能用上线程局部变量, <code class="prettyprint prettyprinted" style="font-family:Consolas,Menlo,Monaco,&#34;Lucida Console&#34;,&#34;Liberation Mono&#34;,&#34;DejaVu Sans Mono&#34;,&#34;Bitstream Vera Sans Mono&#34;,&#34;Courier New&#34;,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,&#34;Lucida Console&#34;,&#34;Liberation Mono&#34;,&#34;DejaVu Sans Mono&#34;,&#34;Bitstream Vera Sans Mono&#34;,&#34;Courier New&#34;,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,&#34;Lucida Console&#34;,&#34;Liberation Mono&#34;,&#34;DejaVu Sans Mono&#34;,&#34;Bitstream Vera Sans Mono&#34;,&#34;Courier New&#34;,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)">&lt;pthread.h&gt;</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
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP