diff options
author | Spencer Dixon <spencercdixon@gmail.com> | 2021-07-02 08:05:07 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-02 17:52:45 +0200 |
commit | 48731e9f17262073b5f99ba113893519af2f938d (patch) | |
tree | d14c1e5ccef44ff798a381fcd17a6ed171b760fb /Userland/Libraries/LibThreading/Thread.cpp | |
parent | 56668098891b7206f7f9bd336fefd115008d2044 (diff) | |
download | serenity-48731e9f17262073b5f99ba113893519af2f938d.zip |
LibThreading: Add new detach() API to Thread
Sometimes you don't care about `joining()` the result of a thread. The
underlying pthread implementation already existed for detaching and
now we expose it to the higher level API.
Diffstat (limited to 'Userland/Libraries/LibThreading/Thread.cpp')
-rw-r--r-- | Userland/Libraries/LibThreading/Thread.cpp | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/Userland/Libraries/LibThreading/Thread.cpp b/Userland/Libraries/LibThreading/Thread.cpp index 71c6c4f45e..2d0da5c118 100644 --- a/Userland/Libraries/LibThreading/Thread.cpp +++ b/Userland/Libraries/LibThreading/Thread.cpp @@ -20,7 +20,7 @@ Threading::Thread::Thread(Function<intptr_t()> action, StringView thread_name) Threading::Thread::~Thread() { - if (m_tid) { + if (m_tid && !m_detached) { dbgln("Destroying thread \"{}\"({}) while it is still running!", m_thread_name, m_tid); [[maybe_unused]] auto res = join(); } @@ -46,3 +46,13 @@ void Threading::Thread::start() } dbgln("Started thread \"{}\", tid = {}", m_thread_name, m_tid); } + +void Threading::Thread::detach() +{ + VERIFY(!m_detached); + + int rc = pthread_detach(m_tid); + VERIFY(rc == 0); + + m_detached = true; +} |