Linux thread exit method and thread resource cleanup

Keywords: thread resource release, pthread_join()

1. Each thread has its own set of registers, its own stack space, and its own errno variable;

2, pthread_create thread creation does not guarantee which thread runs first, the new thread or the calling thread.

3. There are three ways for a thread to exit normally: the thread function returns directly, is canceled by other threads in the same process calling pthread_cancel, the thread exits by calling pthread_exit, (the thread exits abnormally).

4. Whether it is a foreseeable thread termination or an abnormal termination, there will be a problem of resource release. Without considering the exit due to a running error, how to ensure that the thread can successfully release the resources occupied by itself when it terminates, especially Locking resources is a problem that must be considered and solved. The article https://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part4/ introduces pthread_cleanup_push()/pthread_cleanup_pop() to solve the problem of resource release, because Not used, not written here, for the case of abnormal exit, pay attention to adding resources at each exit point, especially the release of lock resources.

5. There are two ways to release resources when the thread exits normally, according to the attributes of the thread: for the attribute of joinable (default), the memory occupied by the thread will only be released after a thread executes pthread_join() on it ; For the detached state of the attribute, the thread releases the occupied memory resources by itself when it finishes running.

6. There are two ways to set the thread to the detached attribute. One is to set the attribute pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED) when the thread is created; the other is to call pthread_detach() to make the thread detached, and the resources can be automatically released.

7. For a thread whose attribute is joinable, the attribute can be changed to detached by calling pthread_detach(), but the thread whose attribute is detached cannot be changed back to the joinable attribute, that is, pthread_join() cannot be called to release resources.

8. If the thread pthread_detach() is executed, the thread request pthread_join() will return an error. If the thread has already called pthread_join(), calling pthread_detach() will have no effect.

9. The memory occupied by a joinable thread will only be released after a thread executes pthread_join() on it. Therefore, in order to avoid memory leaks, the termination of all threads must either be set to DETACHED, or you need to use pthread_join(). ) to recycle .

10. The pthread_join() function: waits for the thread specified by thread to end in a blocking manner . When the function returns, the resources of the waiting thread are reclaimed. If the thread has ended, the function returns immediately. And the thread specified by thread must be joinable.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813111&siteId=291194637