diff --git a/userspace/libraries/LibC/pthread.cpp b/userspace/libraries/LibC/pthread.cpp index a1a0b901..86b269d7 100644 --- a/userspace/libraries/LibC/pthread.cpp +++ b/userspace/libraries/LibC/pthread.cpp @@ -99,7 +99,7 @@ extern "C" void _pthread_trampoline_cpp(void* arg) { auto info = *static_cast(arg); - info.uthread->id = syscall(SYS_THREAD_GETID); + BAN::atomic_store(info.uthread->id, syscall(SYS_THREAD_GETID)); #if defined(__x86_64__) syscall(SYS_SET_FSBASE, info.uthread); @@ -500,6 +500,10 @@ int pthread_create(pthread_t* __restrict thread, const pthread_attr_t* __restric if (syscall(SYS_THREAD_CREATE, _pthread_trampoline, info, info->uthread->attr.stackaddr, info->uthread->attr.stacksize) == -1) goto pthread_create_error; + // wait for the thread to initialize its id + while (BAN::atomic_load(result->id) == -1) + sched_yield(); + if (thread) *thread = result; return 0;