Win32 How to create a multi-threading method, Difference and contact

Source: Internet
Author: User

WIN32 Multi-Threading is created mainly by:

    1. CreateThread ()

    2. _beginthread () &&_beginthreadex ()

    3. AfxBeginThread ()

    4. CWinThread class

First, Introduction

CreateThread: WIN32 provides the most basic API for creating threads that are used to create a thread on the main thread. Returns a handle handle (kernel object). After the kernel objects are used, it is generally necessary to close, using the CloseHandle () function.

_beginthread () &&_beginthreadex (): _beginthreadex () can simply think of _beginthread () as its simplified version, so more often it is easier to use _beginthread ()

In MSDN, you can see an important note, "for a executable file linked with Libcmt.lib, does not call the Win32 exitthread API; This prevents the Run-time system from reclaiming allocated resources. _endthread and _endthreadex reclaim allocated thread resources and then call ExitThread. "Simple translation means, For the executable program that links Libcmt.lib, do not use Win32 's thread Exit Function (ExitThread), which prevents the runtime system from reclaiming allocated resources and should use _endthread. It can reclaim the allocated thread resources and then call ExitThread. This question does not seem to mention CreateThread (), but in fact, this is often seen in some of the information on the "Do not use CreateThread to create threads, otherwise memory leaks " source.

See http://wenku.baidu.com/view/adede4ec4afe04a1b071dea4.html for more detailed information

AfxBeginThread: This is the AFX series function in MFC, a global function that creates threads in MFC.

Cwinthread:ui thread, can receive message, need to call AfxBeginThread create thread.

AfxBeginThread (Runtime_class (MyThread))
Second, some parameters introduced

Dwstacksize: Thread stack size, using 0 with default settings, defaults to 1024K, so you can only create less than 2048 threads (2G of memory) by default. Windows dynamically increases the stack size as needed.

Lpthreadattributes: Thread properties.

Lpstartaddress: A pointer to a thread function.

Lpparameter: The arguments passed to the thread function.

dwCreationFlags: Thread Flag, create_suspended represents the creation of a suspended thread, 0 means that the thread is activated immediately after creation.

Lpthreadid, first thread ID (output parameter)


Win32 How to create a multi-threading method, Difference and contact

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.