boost的try_lock概念的思考
boost的try_lock概念的思考
boost中有个try_lock的概念, 在官方文档里看到。 于是到处搜索windows下try_lcok的实现。
发现一博客里提到:
http://blog.csdn.net/weiwangchao_/article/details/7938892
是用waitforsingleobject(句柄, 0)来实现的。
看完之后,失望之极, 不就换个参数而已,搞这种噱头。 失望原因:try_lock 的意义是什么》
waitforsingleobject是不会消耗cpu的, 这种所谓的try_lock的优势在哪里?或者应用场景在哪里?
这是我的第一个问题。
第2个问题:
从1.32版本文档里看到的:
http://www.boost.org/doc/libs/1_32_0/doc/html/threads/concepts.html#threads.concepts.ScopedTryLock
TryLock Concept
A TryLock is a refinement of Lock. For a TryLock type L and an object lk of that type, the following expressions must be well-formed and
have the indicated effects.
Table 10.7. TryLock Expressions
Expression Effects
lk.try_lock()
Throws boost::lock_error if locked().
Makes a non-blocking attempt to lock the associated mutex object, returning true if the lock attempt is successful, otherwise false.
If the associated mutex object is already locked
by the same thread the behavior is dependent on the locking strategy of the associated mutex object.
1.32中有这么几个锁的策略(http://www.boost.org/doc/libs/1_32_0/doc/html/threads/concepts.html#threads.concepts.locking-strategies)
Locking Strategies
Recursive Locking Strategy
Checked Locking Strategy
Unchecked Locking Strategy
Unspecified Locking Strategy
我的第二个问题是:
一般的boost::mutex::lock , boost::mutext::unlock是属于哪一种锁的策略? 是属于
Unspecified Locking Strategy吗 ?