织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>杂项技术>VC(MFC)>

线程同步

罗索客 发布于 2006-03-06 16:43 点击:次 
UsingCriticalSectionObjectsbr/Thefollowingexampleshowshowathreadinitializes,enters,br/andleavesacriticalsection.br/Aswiththemutexexample(seeUsingMutexObjects),br/thisexampleusesstructuredexception-handlingsyntaxtoensurebr/thatthethreadcallstheLeaveCr
TAG:

Using Critical Section Objects<br/>The following example shows how a thread initializes, enters, <br/>and leaves a critical section.<br/>As with the mutex example (see Using Mutex Objects), <br/>this example uses structured exception-handling syntax to ensure <br/>that the thread calls the LeaveCriticalSection function to release its ownership of the critical section object.  <p></p><p>// Global variable<br/>CRITICAL_SECTION CriticalSection; </p><p>void main()<br/>{<br/>    ...</p><p>    // Initialize the critical section one time only.<br/>    InitializeCriticalSection(&CriticalSection); </p><p>    ...</p><p>    // Release resources used by the critical section object.<br/>    DeleteCriticalSection(&CriticalSection)<br/>}</p><p>DWORD WINAPI ThreadProc( LPVOID lpParameter )<br/>{<br/>    ...</p><p>    // Request ownership of the critical section.<br/>    __try <br/>    {<br/>        EnterCriticalSection(&CriticalSection); </p><p>        // Access the shared resource.<br/>    }<br/>    __finally <br/>    {<br/>        // Release ownership of the critical section.<br/>        LeaveCriticalSection(&CriticalSection);<br/>    }</p><p>    ...</p><p>}<br/></p> (admin)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/200603/566.html]
本文出处: 作者:admin
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容