Documentation Home >> Headers >> <jss/synchronized_value.hpp> Header >> jss::apply function template

The apply function template allows you to pass the protected values from one or more jss::synchronized_value<> objects to a function.

template<typename Func,typename ... ValueTypes>
decltype(std::declval<Func&>()(std::declval<ValueTypes&>()...))
apply(Func&& f,synchronized_value<ValueTypes>& ... sv);

Requires:

A single instance of jss::synchronized_value<> shall not be passed more than once to the same invocation of apply. The function or callable object f shall not call apply directly or indirectly passing any of the jss::synchronized_value<> objects in sv.

Effects:

Acquires the lock for each jss::synchronized_value<> object in sv as-if std::lock(sv.internal_mutex...). Then invokes f(data...), where each element in data is the protected value for the corresponding element in sv, and returns the result to the caller. When the invocation of f returns or exits via exception, any internal locks acquired for this call to apply are released prior to control leaving apply.

Returns:

The value returned from the invocation of f.

Throws:

Any exception throw by f. std::system_error if any of the locks could not be acquired.

Synchronization:

Multiple threads may call apply concurrently without external synchronization. If multiple threads call apply concurrently, and the same jss::synchronized_value<> instance is provided to each of the calls, then the behaviour is as-if the calls were made in some unspecified order. The completion of the full expression associated with one call to apply synchronizes with a subsequent access to the protected value of the same jss::synchronized_value<> object.

Note:

The returned reference must not be accessed after *this has been destroyed.

Header

#include <jss/synchronized_value.hpp>

See Also