Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

c++11 - boost::lock_guard vs boost::mutex::scoped_lock

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.

question from:https://stackoverflow.com/questions/2276805/boostlock-guard-vs-boostmutexscoped-lock

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Amit is right: boost::mutex::scoped_lock is a typedef for boost::unique_lock<boost::mutex>, not lock_guard. scoped_lock is not available in C++0x.

Unless you need the flexibility of unique_lock, I would use lock_guard. It is simpler, and more clearly expresses the intent to limit the lock to a defined scope.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...