refactor: Make MDRecorder join the run thread in destructor

This wasn't broken, but detached threads bother me.
This commit is contained in:
John Colvin committed 2025-03-10 03:05:59 +00:00
1 parent b555850015
commit b9fe39f6e3
1 file changed
+11 -1
+11 -1
View File
@@ -143,6 +143,7 @@ struct MDRecorder::PImpl {
uintptr_t mdc_id = 0;
bts::md::MDClient &md_client;
std::map<SecurityKey, MDRecordingSecEntry> handles;
std::optional<std::thread> run_thread;
MDBatchWriter batch_writer{settings.batch_size,std::vector<bts::md::recording::MDBatchWriter::DBWriteFunction>{DbWriters::writeToSQL}};
uint64_t start_ns;
PImpl(
@@ -160,6 +161,14 @@ struct MDRecorder::PImpl {
1000 * 1000 * 1000,
100000, 1000000000,recording_config.batch_size()}),
md_client(md_client),start_ns(bts::core::TimeStamp::now_ns()) {}
~PImpl() {
active = false;
if (run_thread && run_thread->joinable()) {
run_thread->join();
}
run_thread.reset();
}
};
MDRecorder::MDRecorder(
@@ -171,7 +180,8 @@ MDRecorder::MDRecorder(
me->file.open(file_name, std::ios::out | std::ios::binary | std::ios::trunc);
LOG(md, Info, "MDRecorder::MDRecorder venue: {} file: {}",
api::Venue_Name(venue), file_name);
bts::threading::make_thread("MDRecorder", [&]() { _poll_md(); }).detach();
me->run_thread.emplace(
bts::threading::make_thread("MDRecorder", [&]() { _poll_md(); }));
}
MDRecorder::~MDRecorder() {}