Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-26 09:01:12

0001 /***********************************************************************************\
0002 * (c) Copyright 2024 CERN for the benefit of the LHCb and ATLAS collaborations      *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #pragma once
0012 
0013 #include <chrono>
0014 #include <functional>
0015 #include <future>
0016 #include <thread>
0017 
0018 namespace Gaudi::Utils {
0019   /** Helper to periodically run asynchronous tasks.
0020    *
0021    * An instance of this starts a dedicated thread that periodically runs the user
0022    * specified callback.
0023    *
0024    * The thread starts as soon as the instance is created and it's automatically terminated
0025    * (and joined) on destruction.
0026    * It's also possible to defer the start (if requested at construction time) and
0027    * anticipate the stop.
0028    */
0029   class PeriodicAction {
0030   public:
0031     using clock      = std::chrono::system_clock;
0032     using time_point = clock::time_point;
0033     using callback_t = std::function<void()>;
0034 
0035     PeriodicAction( callback_t callback, std::chrono::milliseconds period_duration, bool autostart = true );
0036     PeriodicAction( PeriodicAction&& )            = default;
0037     PeriodicAction& operator=( PeriodicAction&& ) = default;
0038 
0039     ~PeriodicAction();
0040 
0041     void start();
0042     void stop();
0043 
0044   private:
0045     std::thread               m_thread;
0046     std::promise<void>        m_stop_thread;
0047     callback_t                m_callback;
0048     std::chrono::milliseconds m_period_duration;
0049   };
0050 } // namespace Gaudi::Utils