diff --git a/src/common/Thread.cpp b/src/common/Thread.cpp index 1ec45af1..179b26a2 100644 --- a/src/common/Thread.cpp +++ b/src/common/Thread.cpp @@ -58,7 +58,7 @@ bool Thread::run() } /// -/// +/// Make calling thread wait for termination of the thread. /// void Thread::wait() { @@ -66,7 +66,7 @@ void Thread::wait() } /// -/// +/// Set thread name visible in the kernel and its interfaces. /// /// void Thread::setName(std::string name) @@ -80,6 +80,19 @@ void Thread::setName(std::string name) #endif // _GNU_SOURCE } +/// +/// Indicate that the thread is never to be joined with wait(). +/// The resources of thread will therefore be freed immediately when it +/// terminates, instead of waiting for another thread to perform wait() +/// on it. +/// +void Thread::detach() +{ + if (!m_started) + return; + ::pthread_detach(m_thread); +} + /// /// /// @@ -94,7 +107,7 @@ void Thread::sleep(uint32_t ms) // --------------------------------------------------------------------------- /// -/// +/// Internal helper thats used as the entry point for the thread. /// /// /// diff --git a/src/common/Thread.h b/src/common/Thread.h index 0a75292c..84c29116 100644 --- a/src/common/Thread.h +++ b/src/common/Thread.h @@ -39,19 +39,25 @@ public: /// User-defined function to run for the thread main. virtual void entry() = 0; - /// + /// Make calling thread wait for termination of the thread. virtual void wait(); - /// + /// Set thread name visible in the kernel and its interfaces. virtual void setName(std::string name); + /// Indicate that the thread is never to be joined with wait(). + /// The resources of thread will therefore be freed immediately when it + /// terminates, instead of waiting for another thread to perform wait() + /// on it. + virtual void detach(); + /// static void sleep(uint32_t ms); private: pthread_t m_thread; - /// + /// Internal helper thats used as the entry point for the thread. static void* helper(void* arg); public: