mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 19:44:58 +01:00
fix(physicslog): the writer's destructor throws nothing, its wait a plain condition variable woken to stop; a driver of no vehicle controls nothing
This commit is contained in:
@@ -40,7 +40,11 @@ void keep_last(std::string &Text, std::size_t const Most) {
|
||||
physicslog_writer::physicslog_writer(std::filesystem::path Folder, std::chrono::milliseconds const Interval) : m_folder{std::move(Folder)}, m_interval{Interval} {}
|
||||
|
||||
physicslog_writer::~physicslog_writer() {
|
||||
stop();
|
||||
// at the end of the process: what the logs couldn't keep, memory or a file, isn't to end it worse
|
||||
try {
|
||||
stop();
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
|
||||
void physicslog_writer::begin(std::string const &Name, std::string_view const Header) {
|
||||
@@ -94,6 +98,9 @@ void physicslog_writer::stop() {
|
||||
}
|
||||
if (thread.joinable()) {
|
||||
thread.request_stop();
|
||||
// the writer waits under the lock: taken once, it's either about to wait, seeing the stop, or waiting already
|
||||
{ std::scoped_lock lock{m_mutex}; }
|
||||
m_wake.notify_all();
|
||||
thread.join();
|
||||
}
|
||||
flush();
|
||||
@@ -111,7 +118,7 @@ void physicslog_writer::run(std::stop_token const &Stop) {
|
||||
{
|
||||
std::unique_lock lock{m_mutex};
|
||||
// woken early only to stop, which writes the rest itself
|
||||
if (m_wake.wait_for(lock, Stop, m_interval, [] { return false; }) || Stop.stop_requested())
|
||||
if (m_wake.wait_for(lock, m_interval, [&Stop] { return Stop.stop_requested(); }))
|
||||
return;
|
||||
}
|
||||
flush();
|
||||
|
||||
@@ -63,7 +63,7 @@ private:
|
||||
std::mutex m_mutex; // the lines kept and the thread
|
||||
std::map<std::string, pending> m_pending;
|
||||
bool m_stopped{false};
|
||||
std::condition_variable_any m_wake;
|
||||
std::condition_variable m_wake;
|
||||
std::jthread m_thread;
|
||||
std::thread::id m_writer; // of the thread, kept after it is taken to be joined: the writer knows itself by it
|
||||
std::mutex m_filemutex; // the files are written one batch at a time, so the lines of a log stay in order
|
||||
|
||||
Reference in New Issue
Block a user