Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

With Lock Guard

Non Member Function with_lock_guard
// #include <boost/thread/with_lock_guard.hpp>

namespace boost
{
  template <class Lockable, class Function, class... Args>
  auto with_lock_guard(Lockable& m, Function&& func, Args&&... args) -> decltype(func(boost::forward<Args>(args)...));
}
template <class Lockable, class Function, class... Args>
auto with_lock_guard(
    Lockable& m,
    Function&& func,
    Args&&... args
) -> decltype(func(boost::forward<Args>(args)...));

Precondition:

m must be in unlocked state

Effects:

call func in scope locked by m

Returns:

Result of func(args...) call

Throws:

Any exception thrown by the call to m.lock and func(args...)

Postcondition:

m is in unlocked state

Limitations:

Without c++11 variadic templates support number of arguments is limited to 4

Without rvalue references support calling class method with boost::bind must be const

For correct work with lambda macro BOOST_RESULT_OF_USE_DECLTYPE may be needed to define


PrevUpHomeNext