LibThread: Give Thread std::jthread semantics

Because pthread_create will always call pthread_exit internally before
exiting the thread function, we can remove the odd requirement that the
user's thread function must call Thread::quit internally.

Make Thread::join clear m_tid on success, and print to stderr on
failure. Call join from ~Thread(). Now if you write an infinite loop in
your thread in an application and don't have an exit condition, you will
block in the thread's destructor forever. Time for stop_token? :)
This commit is contained in:
Andrew Kaster 2020-12-31 00:57:44 -07:00 committed by Andreas Kling
parent 2b3993b008
commit 644f5ec160
Notes: sideshowbarker 2024-07-19 00:18:07 +09:00

View File

@ -26,6 +26,7 @@
#include <LibThread/Thread.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
LibThread::Thread::Thread(Function<int()> action, StringView thread_name)
@ -39,7 +40,7 @@ LibThread::Thread::~Thread()
{
if (m_tid) {
dbg() << "trying to destroy a running thread!";
ASSERT_NOT_REACHED();
join();
}
}
@ -66,7 +67,11 @@ void LibThread::Thread::start()
void LibThread::Thread::join()
{
pthread_join(m_tid, nullptr);
int rc = pthread_join(m_tid, nullptr);
if (rc == 0)
m_tid = 0;
else
warnln("pthread_join: {}", strerror(rc));
}
void LibThread::Thread::quit(void* code)