CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
edm::CallNTimesNoWait Class Reference

#include "CallNTimesNoWait.h"

Public Member Functions

 CallNTimesNoWait (unsigned short iNTimes)
 
template<typename T >
void operator() (T iCall)
 

Private Attributes

std::atomic< bool > m_done
 
std::atomic< int > m_ntimes
 

Detailed Description

Description: Thread safe way to do something N times

Usage: This class allows one to safely do a 'non-side-effect' operation N times in a job. An example use would be

void myFunc( int iValue) {
static CallNTimesNoWait message{2};
message([&]() { edm::LogInfo("IWantToKnow")<<"called with "<<iValue; } );

The important thing to remember, is there is no guarantee that the operation being run finishes before a thread which doesn't get to run the operation reaches the code following the call. Therefore it is useful to suppress messages but should not be used to do something like filling a container with values since the filling is not guaranteed to complete before another thread skips the call.

Definition at line 38 of file CallNTimesNoWait.h.

Constructor & Destructor Documentation

◆ CallNTimesNoWait()

edm::CallNTimesNoWait::CallNTimesNoWait ( unsigned short  iNTimes)
inline

Definition at line 40 of file CallNTimesNoWait.h.

40 : m_ntimes(static_cast<int>(iNTimes) - 1), m_done(false) {}

Member Function Documentation

◆ operator()()

template<typename T >
void edm::CallNTimesNoWait::operator() ( T  iCall)
inline

Definition at line 43 of file CallNTimesNoWait.h.

43  {
44  if (not m_done.load(std::memory_order_acquire)) {
45  if (m_ntimes.fetch_sub(1, std::memory_order_acq_rel) < 0) {
46  m_done.store(true, std::memory_order_release);
47  return;
48  };
49  iCall();
50  }
51  }

References m_done, and m_ntimes.

Member Data Documentation

◆ m_done

std::atomic<bool> edm::CallNTimesNoWait::m_done
private

Definition at line 55 of file CallNTimesNoWait.h.

Referenced by operator()().

◆ m_ntimes

std::atomic<int> edm::CallNTimesNoWait::m_ntimes
private

Definition at line 54 of file CallNTimesNoWait.h.

Referenced by operator()().

edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::CallNTimesNoWait::CallNTimesNoWait
CallNTimesNoWait(unsigned short iNTimes)
Definition: CallNTimesNoWait.h:40
edm::CallNTimesNoWait::m_ntimes
std::atomic< int > m_ntimes
Definition: CallNTimesNoWait.h:54
edm::CallNTimesNoWait::m_done
std::atomic< bool > m_done
Definition: CallNTimesNoWait.h:55