How to correctly create threads and End threads (1)

Source: Internet
Author: User

Recently, memoryleak has appeared in a very old product in the company. Now we have a lot of people working on it. We have accumulated something to share with you during the investigation process ~

1> How to Create a thread correctly?

 

Comparison of the created thread functions in Win32.

 

Function

Provider

Applicability

_ Beginthread ()

C/C ++ provides

C/C ++ Program

_ Beginthreadex ()

C/C ++ provides

C/C ++ Program

CreateThread ()

Windows

Windows program

※1) Memory leakage occurs when C/C ++ calls CreateThread (). We recommend that you use _ beginthreadex.

2) When a thread fails to be created, the return values of __ininthread () and _ beginthreadex () are different. _ Beginthread () is-1 ,__ beginthreadex () is 0.

3) when the thread exits, __beginthread () will call _ endthread (), this function will release the _ tidata block of the thread, and then call ExitThread (), and finally CloseHandle (). _ Begintheadex () calls _ endtheadex (). This function is different from _ endthread, but does not close the thread handle.

4) if _ beginthread () is called to create a thread, the subsequent Code does not involve processing of the thread handle.

For example, it is not advisable to use the WaitForSingleObject function to determine the end of the thread created by _ beginthread.

For details, see Windows core programming

Related Article

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.