线程同步-临界区

 

   1.临界区(Critical Section)
 A synchronization object that allows one thread at a time to access a resource or section of code. Critical sections are useful when only one

thread at a time can be allowed to modify data or some other controlled resource. For example, adding nodes to a linked list is a process that should

only be allowed by one thread at a time.
 Critical sections are used instead of mutexes when speed is critical and the resource will not be used across process boundaries.
 一种【同步】对象,在一个时刻只允许一个线程访问临界资源或临界代码段。
 当需要更快的速度时可用临界区代替互斥量,注意:临界资源只允许在一个进程中使用。
 Critical Sections(临界段),源代码中如果有不能由两个或两个以上线程同时执行的部分,可以用临界段来使这部分的代码执行串行化。它只能在一个独立的

进程或一个独立的应用程序中使用。
 
优点:
 速度快,原因是:临界区是用户方式的同步,其他是内核。(原因是什么呢?计算量小?)
缺点:
 临界区不是内核对象,只能用于进程内部的线程同步。

临界区锁

VC可使用 CCriticalSection 参见atlmfc\include\afxmt.h

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值