CMS 3D CMS Logo

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 
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
static std::mutex mutex
Definition: Proxy.cc:8
const Functor functor_
#define CMS_THREAD_SAFE
std::invoke_result_t< Functor, Params... > operator()(Params &&... params) const
ThreadSafeFunctor(ThreadSafeFunctor &&other) noexcept
def move(src, dest)
Definition: eostools.py:511
ThreadSafeFunctor(Params &&... params)