diff --git a/src/threadloop.cpp b/src/threadloop.cpp index 5fbb75038681dffbe569595496c52ae691328de8..140f1deccb1f4fdd66abf4f07d2ce3d14ab1271f 100644 --- a/src/threadloop.cpp +++ b/src/threadloop.cpp @@ -24,10 +24,12 @@ namespace ring { void -ThreadLoop::mainloop(const std::function<bool()> setup, +ThreadLoop::mainloop(std::thread::id& tid, + const std::function<bool()> setup, const std::function<void()> process, const std::function<void()> cleanup) { + tid = std::this_thread::get_id(); try { if (setup()) { while (state_ == RUNNING) @@ -37,7 +39,9 @@ ThreadLoop::mainloop(const std::function<bool()> setup, RING_ERR("setup failed"); } } catch (const ThreadLoopException& e) { - RING_ERR("%s", e.what()); + RING_ERR("[threadloop:%p] ThreadLoopException: %s", this, e.what()); + } catch (const std::exception& e) { + RING_ERR("[threadloop:%p] Unwaited exception: %s", this, e.what()); } } @@ -85,7 +89,8 @@ ThreadLoop::start() } state_ = RUNNING; - thread_ = std::thread(&ThreadLoop::mainloop, this, setup_, process_, cleanup_); + thread_ = std::thread(&ThreadLoop::mainloop, this, std::ref(threadId_), setup_, process_, cleanup_); + threadId_ = thread_.get_id(); } void @@ -124,7 +129,7 @@ ThreadLoop::isStopping() const noexcept std::thread::id ThreadLoop::get_id() const noexcept { - return thread_.get_id(); + return threadId_; } void diff --git a/src/threadloop.h b/src/threadloop.h index d19fb48b7df7b5fd6879e64d7f12973e3c19449c..21f5e32d8e6edfb56923d14f833d770c2af88e22 100644 --- a/src/threadloop.h +++ b/src/threadloop.h @@ -66,11 +66,13 @@ private: std::function<void()> process_; std::function<void()> cleanup_; - void mainloop(const std::function<bool()> setup, + void mainloop(std::thread::id& tid, + const std::function<bool()> setup, const std::function<void()> process, const std::function<void()> cleanup); std::atomic<ThreadState> state_ {READY}; + std::thread::id threadId_; std::thread thread_; };