Differences between createthread and _ beginthread

Source: Internet
Author: User
Tags terminates

Createthread is a win 32api function, and _ beginthread is a CRT (C Run-Time) function. They are all functions created to implement multiline city, and they have the same usage method, the same parameter list.

But what are their differences?

In general, there is not much difference from the perspective of use. Except for the signal () function in the CRT function, it cannot be used in the linear city created by createthread. Other CRT functions can be used normally, however, if you use the CRT function in the line city created by createthread, some memory leak will be generated.

Here is the original Excerpt from KB:

Summary

All C run-time functions t the signal () function work correctly when used in threads created by the createthread () function. however, depending on what CRT functions are called, there may be a small memory leak when threads are terminated. calling strlen (), for example, does not trigger the allocation of the CRT thread data-block, and calling malloc (), fopen (), _ open (), strtok (), ctime (), or localtime () causes allocation of a CRT per-thread data-block, which may cause a memory leak.

More information

The "programming techniques" manual supplied with Visual C ++ 32-bit edition states that using createthread () in a program that uses libcmt. lib causes uninstall CRT functions to fail. actually, the only function that shocould not be used in a thread created with createthread () is the signal () function.

There are two ways to create threads. one method involves using the CRT _ beginthread () or _ beginthreadex () (with Visual C ++ 2.0 and later); the other method involves using the createthread () API. all CRT functions other than the signal () function work correctly in threads created with either _ beginthread () or createthread (). however, there are some problems involved with using CRT functions in threads created with createthread ().

Threads that are created and terminated with the createthread () and exitthread () win32 API functions do not have memory that is allocated by the CRT for static data and static buffers cleaned up when the thread terminates. some examples of this type of memory are static data for errno and _ doserrno and the static buffers used by functions such as asctime (), ctime (), localtime (), gmtime (), and mktime (). using createthread () in a program that uses the CRT (for example, links with libcmt. lib) may cause a memory leak of about 70-80 bytes each time a thread is terminated.

To guarantee that all static data and static buffers allocated by the CRT are cleaned up when the thread terminates, _ beginthreadex () and _ endthreadex () shocould be used when creating a thread. the _ beginthreadex () function provided des the same parameters and functionality as createthread (). in addition, there is a quiz: Can the wire city created with createthread be disabled by the CRT function _ endthreadex?

 

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.