CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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) {
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

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

Definition at line 42 of file CallNTimesNoWait.h.

42 : m_ntimes(static_cast<int>(iNTimes)-1), m_done(false){}
std::atomic< int > m_ntimes
std::atomic< bool > m_done

Member Function Documentation

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

Definition at line 45 of file CallNTimesNoWait.h.

References m_done, and m_ntimes.

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

Member Data Documentation

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

Definition at line 57 of file CallNTimesNoWait.h.

Referenced by operator()().

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

Definition at line 56 of file CallNTimesNoWait.h.

Referenced by operator()().