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
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
ThreadSafeFunctor::functor_
const Functor functor_
Definition: ThreadSafeFunctor.h:26
trackingPlots.other
other
Definition: trackingPlots.py:1467
CMS_THREAD_SAFE
#define CMS_THREAD_SAFE
Definition: thread_safety_macros.h:4
ThreadSafeFunctor
Definition: ThreadSafeFunctor.h:12
thread_safety_macros.h
mutex
static std::mutex mutex
Definition: Proxy.cc:8
ThreadSafeFunctor::mutex_
std::mutex mutex_
Definition: ThreadSafeFunctor.h:27
eostools.move
def move(src, dest)
Definition: eostools.py:511
ThreadSafeFunctor::ThreadSafeFunctor
ThreadSafeFunctor(Params &&... params)
Definition: ThreadSafeFunctor.h:15
ThreadSafeFunctor::operator()
std::invoke_result_t< Functor, Params... > operator()(Params &&... params) const
Definition: ThreadSafeFunctor.h:20
ThreadSafeFunctor::ThreadSafeFunctor
ThreadSafeFunctor(ThreadSafeFunctor &&other) noexcept
Definition: ThreadSafeFunctor.h:17