CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ThreadSafeFunctor.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_ThreadSafeFunctor_H
2 #define CommonTools_Utils_ThreadSafeFunctor_H
3 
5 
6 #include <mutex>
7 #include <utility>
8 
9 // This class is a simple wrapper around some functor class to use its operator() in a thread-safe way.
10 
11 template <class Functor>
13 public:
14  template <typename... Params>
15  ThreadSafeFunctor(Params&&... params) : functor_{std::forward<Params>(params)...} {}
16 
17  ThreadSafeFunctor(ThreadSafeFunctor&& other) noexcept : functor_(std::move(other.functor_)) {}
18 
19  template <typename... Params>
20  typename std::invoke_result_t<Functor, Params...> operator()(Params&&... params) const {
21  std::lock_guard<std::mutex> guard(mutex_);
22  return functor_(std::forward<Params>(params)...);
23  }
24 
25 private:
26  const Functor functor_;
28 };
29 
30 #endif
ThreadSafeFunctor(Params &&...params)
static std::mutex mutex
Definition: Proxy.cc:8
const Functor functor_
def move
Definition: eostools.py:511
#define CMS_THREAD_SAFE
std::invoke_result_t< Functor, Params...> operator()(Params &&...params) const
ThreadSafeFunctor(ThreadSafeFunctor &&other) noexcept