线程库如何知道线程已完成其操作?

How does a threading library know that a thread has finished its operation?

无论是什么,无论是 OpenMP 还是 posix 线程,这些库如何知道线程何时完成其工作?

线程显式,(调用一些 'exitThread() API),或隐式,(returns 来自线程函数的顶层,因此隐式导致 exitThread 调用),告诉 OS 不再给它 CPU 并释放它的资源。

在大多数线程库中,您为线程提供的“主”例程并不是真正的线程“主”例程。一个新线程通常从执行一些库代码开始,然后 然后 它调用您的代码,最后在您的代码“完成”后调用更多库代码。例如,

real_main_routine(caller_provided_main_routine) {
    initialize_stuff();
    try {
        caller_provided_main_routine();
    } catch (exception e) {
        maybe_do_something_with(e);
    }
    clean_stuff_up_and_die();
}

clean_stuff_up_and_die() 中的某处是库告诉自己线程已完成的地方。