Note that there are some explanatory texts on larger screens.

plurals
  1. POshould I make member functions that work with an adjustable clock static?
    primarykey
    data
    text
    <p>I have the following two headers.</p> <pre><code>#ifndef DRD_EVENT_HPP #define DRD_EVENT_HPP #include &lt;functional&gt; namespace drd { template &lt;typename Clock&gt; class event { public: using clock = Clock; using time_point = typename clock::time_point; template &lt;class F, class... Args&gt; event(time_point et, F&amp;&amp; f, Args&amp;&amp;... args) : Task(std::bind&lt;void&gt;(std::forward&lt;F&gt;(f), std::forward&lt;Args&gt;(args)...)), Time(et) {} void perform() ///Can throw std::bad_function_call { Task(); Task = nullptr; } ///Returns the event time. time_point time() const noexcept { return Time; } ///Checks if the event has not been performed yet. bool pending() const noexcept { return static_cast&lt;bool&gt;(Task); } private: std::function&lt; void()&gt; Task; time_point Time; }; struct later_event { template &lt;class Clock&gt; bool operator()(const event&lt;Clock&gt;&amp; lhs, const event&lt;Clock&gt;&amp; rhs) const noexcept { return lhs.time() &gt; rhs.time(); } }; } #endif // DRD_EVENT_HPP #ifndef DRD_DISCRETE_SIMULATION_HPP #define DRD_DISCRETE_SIMULATION_HPP #include &lt;exception&gt; #include &lt;chrono&gt; #include &lt;queue&gt; #include "event.hpp" namespace drd { namespace des ///Discrete event simulation { template &lt;class Rep, class Period = std::ratio&lt;1&gt;&gt; class simulation_engine { public: class clock; using time_point = typename clock::time_point; using duration = typename clock::duration; using event_type = event&lt;clock&gt;; public: ///Constructs an event "in-place" and inserts it in the events list template &lt;typename... EventArgs&gt; void schedule(EventArgs&amp;&amp;... event_args_) { EventsList.emplace(std::forward&lt;EventArgs&gt;(event_args_)...); } bool has_pending_events() const noexcept { return not EventsList.empty(); } ///Advances the clock until the next event time and then the event ///is performed, if the events list was empty the behavior is undefined. void next_event() { auto Event = EventsList.top(); EventsList.pop(); clock::advance_until(Event.time()); Event.perform(); } ///Calls next_event() while the events list is not empty. void simulate() { while (has_pending_events()) next_event();} ///Performs all of events whose time is scheduled before or just at the ///moment t, then advances the clock until t. void simulate_until(time_point t) { while(has_pending_events() and EventsList.top().time() &lt;= t) next_event(); clock::advance_until(t); } void simulate_for(duration d) { simulate_until(clock::now() + d); } private: std::priority_queue&lt;event_type, std::vector&lt;event_type&gt;, later_event&gt; EventsList; }; ///clock type that is thread-independent and adjustable by ///simulation_engine template &lt;class Rep, class Period&gt; class simulation_engine&lt;Rep,Period&gt;::clock { public: using rep = Rep; using period = Period; using duration = std::chrono::duration&lt;rep,period&gt;; using time_point = std::chrono::time_point&lt;clock&gt;; public: static constexpr bool is_steady = false; public: static time_point now() noexcept { return CurrentTime;} private: static void reset() noexcept { CurrentTime = time_point(); } static void adjust(time_point t) noexcept { CurrentTime = t; } static void advance_until(time_point t) { if(t &lt; CurrentTime) throw std::logic_error("advance_until cannot set the clock back."); CurrentTime = t; } friend simulation_engine&lt;Rep,Period&gt;; private: static thread_local time_point CurrentTime; }; template &lt;class Rep, class Period&gt; thread_local typename simulation_engine&lt;Rep,Period&gt;::clock::time_point simulation_engine&lt;Rep,Period&gt;::clock::CurrentTime; } } #endif //DRD_DISCRETE_SIMULATION_HPP </code></pre> <p>I was wondering if I should make member functions of simulation_engine static, because objects own independent events list and share the same clock, and might happen a synchronization problem. </p> <p>What should I do?</p> <p>Sorry for my english and the extensive code. I hope your answer, thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload