Chonkie Mongol
Chonkie Mongol

Reputation: 25

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

Whatever it is, be it OpenMP, or posix threads, how do these libraries know when a thread is done with its job?

Upvotes: 0

Views: 161

Answers (2)

Solomon Slow
Solomon Slow

Reputation: 27115

In most threading libraries, the "main" routine that you provide for a thread isn't really the thread's "main" routine. A new thread typically starts by executing some library code, and then it calls your code, and finally it calls more library code after your code is "finished." E.g.,

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();
}

Somewhere within that clean_stuff_up_and_die() is where the library tells itself that the thread is finished.

Upvotes: 2

Martin James
Martin James

Reputation: 24847

The thread explicitly, (calls some 'exitThread() API), or implicitly, (returns from the top level of the thread function, so implicitly resulting in an exitThread call), tells the OS to give it no more CPU and to release it's resources.

Upvotes: 1

Related Questions