mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 01:17:35 +00: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
1 changed files with 7 additions and 2 deletions
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <LibThread/Thread.h>
|
#include <LibThread/Thread.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
LibThread::Thread::Thread(Function<int()> action, StringView thread_name)
|
LibThread::Thread::Thread(Function<int()> action, StringView thread_name)
|
||||||
|
@ -39,7 +40,7 @@ LibThread::Thread::~Thread()
|
||||||
{
|
{
|
||||||
if (m_tid) {
|
if (m_tid) {
|
||||||
dbg() << "trying to destroy a running thread!";
|
dbg() << "trying to destroy a running thread!";
|
||||||
ASSERT_NOT_REACHED();
|
join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +67,11 @@ void LibThread::Thread::start()
|
||||||
|
|
||||||
void LibThread::Thread::join()
|
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)
|
void LibThread::Thread::quit(void* code)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue