QMutex

本文讨论了多线程环境下确保数据结构或代码段访问串行化的最佳方法,重点介绍了使用QMutexLocker来简化互斥锁的管理。文章还对比了递归与非递归模式下互斥锁的行为,并探讨了不当使用可能引发的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的 :access serialization between threads.

应用对象:an object, data structure or section of code


best to use a mutex with a QMutexLocker since this makes it easy to ensure that locking and unlocking are performed consistently.

说明:如果 lock() 之后,发生了 exception,那么,如果没有 handle 这个 exception,或者 exception 处理中没有 unlock,那么,这个 Mutex 永远处于 lock 的 状态。其他想要 获取 lock 的 thread 如果使用的是 lock() (注意:有 non blocking 的 tryLock( timeOut)  )  , 那么,thread 就会 blocking 在这里。


Recursive Mode vs Non-recursive Mode

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex.

说明:一般,我们都是将同一个 Mutex 给不同的 thread , threads 之间 争取mutex。而 Recursive,则是 在同一个thread中,进行 锁/ 非锁。

如果 non-recursive 的 进行 recursive 式地操作,就会:

dead-lock when the mutex is locked recursively.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值