Thread separation: pthread_detach

Source: Internet
Author: User

The default state of creating a thread is joinable. If a thread stops running but is not joined, its state is similar to zombie process in the process, that is, some resources are not recycled (exit status code). Therefore, the thread Creator should call pthread_join to wait until the thread stops running and obtain the exit code of the thread, reclaim resources (similar to wait and waitpid)

However, after pthread_join (pthread_id) is called, if the thread does not end, the caller will be blocked. In some cases, we do not want this to happen, for example, when the main thread creates a subthread for each new link in the web server for processing, the main thread does not want to block it because it calls pthread_join (because it still needs to process the incoming link). In this case, you can add the code pthread_detach (pthread_self () in the Child thread ())

Or the parent thread calls

Pthread_detach (thread_id) (non-blocking, can be returned immediately)

This sets the sub-thread status to detached, and all resources are automatically released after the thread finishes running.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.