boost::lock_guard 与 boost::mutex::scoped_lock
boost::lock_guard
和 boost::mutex::scoped_lock
哪个是首选?
我正在使用 Boost.Thread,希望在 C++11 线程可用时能够迁移到它。
scoped_lock
是下一个 C++ 标准的一部分吗?
相比另一种,选择其中一种有什么优势吗?
注意:我知道 scoped_lock
只是 lock_guard
的 typedef
。
编辑:我错了 scoped_lock
是 不是 lock_guard
的 typedef
。它是 unique_lock
的 typedef
。
Which is preferred boost::lock_guard
or boost::mutex::scoped_lock
?
I'm using Boost.Thread with the hope to move to C++11 threading when it becomes available.
Is scoped_lock
part of the next c++ standard?
Are the any advantages to prefer one over the other?
NOTE: I'm aware that scoped_lock
is just a typedef
of lock_guard
.
edit: I was wrong scoped_lock
is not a typedef
of lock_guard
. It's a typedef
of unique_lock
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Amit 是对的:
boost::mutex::scoped_lock
是boost::unique_lock
的typedef
,而不是>lock_guard
。scoped_lock
在 C++0x 中不可用。除非您需要
unique_lock
的灵活性,否则我会使用lock_guard
。它更简单,更清楚地表达了将锁限制在定义的范围内的意图。Amit is right:
boost::mutex::scoped_lock
is atypedef
forboost::unique_lock<boost::mutex>
, notlock_guard
.scoped_lock
is not available in C++0x.Unless you need the flexibility of
unique_lock
, I would uselock_guard
. It is simpler, and more clearly expresses the intent to limit the lock to a defined scope.两者差别不大。根据 Boost,
scoped_lock< /code> 是
unique_lock
的类型定义。unique_lock
和lock_guard
都实现了 RAII 风格的锁定。两者之间的区别很简单,unique_lock
有一个更复杂的接口——它允许推迟锁定并调用解锁。Not much difference between the two. As per Boost,
scoped_lock
is a typedef forunique_lock<mutex>
. Both ofunique_lock
andlock_guard
implement RAII-style locking. The difference between is simply thatunique_lock
has a more complex interface -- it allows to defer lock and call unlock.