线程局部存储TLS

论坛 期权论坛     
选择匿名的用户   2021-6-2 17:55   299   0
<p><a href=""><u><span style="color:#0066cc">Thread local storage</span></u></a> (TLS)统一进程的多个线程可以通过由TlsAlloc方法返回的索引值在线程自身的空间内存储和取回一个值。在以下这个例子里,索引值在进程开始时创建,当各个线程启动时,会各自申请一块动态内存并且将内存指针通过TlsSetValue方法存储到各自的TLS空间中(由先前的索引值标定)。CommonFunc方法使用TlsGetValue方法通过索引取得数据指针。在各个线程结束前,释放动态内存块。在进程结束见,调用TlsFree方法释放索引。<br> </p>
<div>
<span style="color:#08080"> 1</span>
<img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif">
<span style="color:#000000">#include </span>
<span style="color:#000000">&lt;</span>
<span style="color:#000000">windows.h</span>
<span style="color:#000000">&gt;</span>
<span style="color:#000000"> <br> </span>
<span style="color:#08080"> 2</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif">#include </span>
<span style="color:#000000">&lt;</span>
<span style="color:#000000">stdio.h</span>
<span style="color:#000000">&gt;</span>
<span style="color:#000000"> <br> </span>
<span style="color:#08080"> 3</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif"> <br> </span>
<span style="color:#08080"> 4</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif"></span>
<span style="color:#00ff">#define</span>
<span style="color:#000000"> THREADCOUNT 4 </span>
<span style="color:#000000"><br> </span>
<span style="color:#08080"> 5</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif">DWORD dwTlsIndex; <br> </span>
<span style="color:#08080"> 6</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif"> <br> </span>
<span style="color:#08080"> 7</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif">VOID ErrorExit(LPSTR); <br> </span>
<span style="color:#08080"> 8</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif"> <br> </span>
<span style="color:#08080"> 9</span>
<span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-cbef093dcc044b2793832001e2365e43.gif">VOID CommonFunc(VOID) <br> </span>
<span style="color:#08080">10</span>
<span style="color:#000000"><img align="top" alt="" id="Codehighlighter1_137_513_Open_Image" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-2f88ce130b654eb5dc6788e02dbcfc90.gif"></span>
<span id="Codehighlighter1_137_513_Open_Text"><span style="color:#000000">{ <br> </span><span style="color:#08080">11</span><span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-df37983f39daa189b8c814e01a6a9011.gif">   LPVOID lpvData; <br> </span><span style="color:#08080">12</span><span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-df37983f39daa189b8c814e01a6a9011.gif"> <br> </span><span style="color:#08080">13</span><span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-df37983f39daa189b8c814e01a6a9011.gif"></span><span style="color:#0800">//</span><span style="color:#0800"> Retrieve a data pointer for the current thread. </span><span style="color:#0800"><br> </span><span style="color:#08080">14</span><span style="color:#0800"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-df37983f39daa189b8c814e01a6a9011.gif"></span><span style="color:#000000"> <br> </span><span style="color:#08080">15</span><span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-df37983f39daa189b8c814e01a6a9011.gif">   lpvData </span><span style="color:#000000">&#61;</span><span style="color:#000000"> TlsGetValue(dwTlsIndex); <br> </span><span style="color:#08080">16</span><span style="color:#000000"><img align="top" alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-df37983f39daa189b8c814e01a6a9011.gif">   </span><span style="color:#00ff">if</span><span style="color:#000000"> ((lpvData </span><span style="color:#000000">&#61;&#61;</span><span style="color:#000000"> </span><span style="color:#000000">0</span><span style="color:#000000">) </span><span style="color:#000000">&amp;&amp;</span><span style="color:#000000"> (GetLastError() </span><span style="color:#000000">!&#61;</span><span style="color:#000000"> ERROR_SUCCESS)) <br> </span><span style="color:#08080">17</span><span style="color:#000000"><img align="top" alt="" src="https://beijingopt
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

下载期权论坛手机APP