mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-09 18:16:09 +03:00
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:
parent
2b3993b008
commit
644f5ec160
Notes:
sideshowbarker
2024-07-19 00:18:07 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/644f5ec160c Pull-request: https://github.com/SerenityOS/serenity/pull/4683
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user