MeshLib C++ Docs
Loading...
Searching...
No Matches
MRAsyncTimer.h
Go to the documentation of this file.
1#pragma once
2
3#include "exports.h"
4#include <mutex>
5#include <condition_variable>
6#include <chrono>
7#include <optional>
8#ifndef __EMSCRIPTEN__
9#include <thread>
10#include <functional>
11#include <memory>
12#endif
13
14namespace MR
15{
18
19
20using Time = std::chrono::time_point<std::chrono::system_clock>;
21
23class MRVIEWER_CLASS AsyncTimer
24{
25public:
27 MRVIEWER_API void setTime( const Time& time );
30 MRVIEWER_API bool setTimeIfNotSet( const Time& time );
32 MRVIEWER_API void resetTime();
34 MRVIEWER_API void terminate();
35
36public:
37 enum class Event
38 {
39 AlertTimeReached,
40 Terminate
41 };
42 MRVIEWER_API Event waitBlocking();
43
44private:
45 std::mutex mutex_;
46 std::condition_variable cvar_;
47 std::optional<Time> time_;
48 bool terminating_ = false;
49};
50
51#ifndef __EMSCRIPTEN__
55class MRVIEWER_CLASS AsyncRequest
56{
57public:
58 MRVIEWER_API AsyncRequest();
59 MRVIEWER_API ~AsyncRequest();
60 using Command = std::function<void()>;
61
64 MRVIEWER_API void request( const Time& time, Command command );
65
68 MRVIEWER_API void requestIfNotSet( const Time& time, Command command );
69
71 MRVIEWER_API void reset();
72private:
73 std::thread listenerThread_;
74 AsyncTimer timer_;
75
76 Command loadCommand_();
77 void storeCommand_( Command command );
78
79 std::mutex cmdMutex_;
80 Command command_;
81
82};
83#endif
84}
Definition MRAsyncTimer.h:56
the object to set timer from any thread and wait for alert time from another thread
Definition MRAsyncTimer.h:24
MRVIEWER_API void requestIfNotSet(const Time &time, Command command)
MRVIEWER_API void request(const Time &time, Command command)
MRVIEWER_API void reset()
clears command
std::chrono::time_point< std::chrono::system_clock > Time
Definition MRAsyncTimer.h:20
MRVIEWER_API void setTime(const Time &time)
sets alert time, forgetting about previous time
std::function< void()> Command
Definition MRAsyncTimer.h:60
MRVIEWER_API AsyncRequest()
MRVIEWER_API ~AsyncRequest()
MRVIEWER_API void resetTime()
reset the timer
MRVIEWER_API Event waitBlocking()
MRVIEWER_API bool setTimeIfNotSet(const Time &time)
Event
to call from waiter thread
Definition MRAsyncTimer.h:38
MRVIEWER_API void terminate()
orders the waiter thread to stop
only for bindings generation
Definition MRCameraOrientationPlugin.h:8