33 #ifndef REALTIME_TOOLS__REALTIME_BUFFER_HPP_
34 #define REALTIME_TOOLS__REALTIME_BUFFER_HPP_
49 non_realtime_data_ =
new T();
50 realtime_data_ =
new T();
61 non_realtime_data_ =
new T(data);
62 realtime_data_ =
new T(data);
67 if (non_realtime_data_) {
68 delete non_realtime_data_;
71 delete realtime_data_;
75 RealtimeBuffer(
const RealtimeBuffer & source)
78 non_realtime_data_ =
new T();
79 realtime_data_ =
new T();
82 writeFromNonRT(*source.readFromNonRT());
90 if (
this == &source) {
95 writeFromNonRT(*source.readFromNonRT());
103 std::unique_lock<std::mutex> guard(mutex_, std::try_to_lock);
104 if (guard.owns_lock()) {
106 if (new_data_available_) {
107 T * tmp = realtime_data_;
108 realtime_data_ = non_realtime_data_;
109 non_realtime_data_ = tmp;
110 new_data_available_ =
false;
113 return realtime_data_;
116 T * readFromNonRT()
const
118 std::lock_guard<std::mutex> guard(mutex_);
120 if (new_data_available_) {
121 return non_realtime_data_;
123 return realtime_data_;
127 void writeFromNonRT(
const T & data)
130 std::lock_guard<std::mutex> guard(mutex_);
132 std::unique_lock<std::mutex> guard(mutex_, std::defer_lock);
133 while (!guard.try_lock()) {
134 std::this_thread::sleep_for(std::chrono::microseconds(500));
139 *non_realtime_data_ = data;
140 new_data_available_ =
true;
143 void initRT(
const T & data)
145 *non_realtime_data_ = data;
146 *realtime_data_ = data;
152 if (non_realtime_data_) {
153 delete non_realtime_data_;
155 if (realtime_data_) {
156 delete realtime_data_;
160 non_realtime_data_ =
new T();
161 realtime_data_ =
new T();
166 T * non_realtime_data_;
167 bool new_data_available_;
170 mutable std::mutex mutex_;