CMS 3D CMS Logo

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

#include "FWCore/Utilities/interface/CallOnceNoWait.h"

Public Member Functions

 CallOnceNoWait ()
 
template<typename T >
void operator() (T iCall)
 

Private Attributes

std::atomic< bool > m_called
 

Detailed Description

Description: Thread safe way to do something 1 time

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

void myFunc( int iValue) {
static CallOnceNoWait message;
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 40 of file CallOnceNoWait.h.

Constructor & Destructor Documentation

◆ CallOnceNoWait()

edm::CallOnceNoWait::CallOnceNoWait ( )
inline

Definition at line 42 of file CallOnceNoWait.h.

42 : m_called(false) {}
std::atomic< bool > m_called

Member Function Documentation

◆ operator()()

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

Definition at line 45 of file CallOnceNoWait.h.

References m_called.

45  {
46  bool expected = false;
47  if (m_called.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) {
48  iCall();
49  }
50  }
std::atomic< bool > m_called

Member Data Documentation

◆ m_called

std::atomic<bool> edm::CallOnceNoWait::m_called
private

Definition at line 53 of file CallOnceNoWait.h.

Referenced by operator()().