Interviewer Question: How can threads be reclaimed in Linux? What are the common methods?
Candidate Reference Answer: In POSIX thread (<span><span>pthread</span></span>) programming, after a thread finishes, some information (such as return value and exit status) is retained for other threads to use. If these resources are not reclaimed, it may lead tothread resource leakage. Common methods for thread reclamation include the following:
1. Use <span><span>pthread_join()</span></span> to wait for and reclaim the thread
int pthread_join(pthread_t tid, void **retval);
-
Function: The main thread calls this function to wait for the specified child thread to finish executing and reclaim its resources;
-
Similar to
<span><span>wait()</span></span>or<span><span>waitpid()</span></span>in processes; -
The thread calling this function willblock until the target thread exits;
-
<span><span>retval</span></span>is used to receive the return value passed back by the child thread through<span><span>pthread_exit()</span></span>; -
Suitable for scenarios where the thread result is needed or confirmation of its termination is required.
2. Use <span><span>pthread_exit()</span></span> to actively terminate the thread
void pthread_exit(void *retval);
-
Function: The thread calls this function to exit actively;
-
Any pointer can be returned as the thread exit value, which can be obtained by
<span><span>pthread_join()</span></span>; -
After calling this function, the thread resources still exist and need to be reclaimed with
<span><span>pthread_join()</span></span>or<span><span>pthread_detach()</span></span>; -
Commonly used to actively terminate the current thread in a child thread.
3. Use <span><span>pthread_detach()</span></span> to set the thread to detached state
int pthread_detach(pthread_t tid);
-
Function: Set the thread to a “detached” state;
-
In detached state, after the thread ends, the system willautomatically release its resources, no need to explicitly call
<span><span>pthread_join()</span></span>; -
Can be called in the main thread using
<span><span>pthread_detach(tid)</span></span>or used within the thread with<span><span>pthread_detach(pthread_self())</span></span>; -
Suitable for scenarios where the return value of the thread is not of concern and there is no intention to reclaim it actively (e.g., background tasks, asynchronous execution);
-
Once set to detached state, it cannot be called again with
<span><span>pthread_join()</span></span>.
4. Summary and Comparison of the Three Methods:
| Method | Blocking | Automatic Reclamation | Can Retrieve Return Value | Applicable Scenarios |
|---|---|---|---|---|
<span><span>pthread_join</span></span> |
Yes | Yes | Yes | Scenarios concerned with thread execution results |
<span><span>pthread_exit</span></span> |
No | No (requires join or detach) | Yes | Child thread exits actively, resources need to be reclaimed |
<span><span>pthread_detach</span></span> |
No | Yes | No | Asynchronous tasks, not concerned with return values |
Choosing the appropriate reclamation method can prevent thread leakage while enhancing the robustness and efficiency of the program.