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