Multithreaded Programming (2)-From CreateThread

Source: Internet
Author: User

function CreateThread(
 lpThreadAttributes: Pointer;      {安全设置}
 dwStackSize: DWORD;          {堆栈大小}
 lpStartAddress: TFNThreadStartRoutine; {入口函数}
 lpParameter: Pointer;         {函数参数}
 dwCreationFlags: DWORD;        {启动选项}
 var lpThreadId: DWORD         {输出线程 ID }
): THandle; stdcall;           {返回线程句柄}

Building a thread on Windows is inseparable from the CreateThread function;

Tthread.create is the first to call the Beginthread (Delphi Custom), beginthread and call CreateThread.

Since there is a build, there should be release, createthread the corresponding release function is: ExitThread, such as the following code:

procedure TForm1.Button1Click(Sender: TObject);
begin
 ExitThread(0); {此句即可退出当前程序, 但不建议这样使用}
end;

Code comments:

The current program is a process, the process is only a working environment, the thread is a worker;

Each process will have a boot thread (or the main line), which means that the bulk of our previous coding is written to the main thread;

The upper ExitThread (0); is to exit the main thread;

The system does not allow a thread-free process to exist, so the program exits.

In addition: The parameter of the ExitThread function is an exit code, this exit code is for the other functions after the, here to give an unsigned integer.

Perhaps you will say: This exitthread is very good; In fact, whether using the API or using TThread class to write multithreading, we rarely use it; Because:

1, if the direct use of API CreateThread, it performs the entry function will automatically exit, without exitthread;

2, with the TThread class established thread and must not use ExitThread exit; Because when you use TThread to create a thread, you assign more resources (such as your custom membership, and the resources allocated by its ancestor class (TObject), and so on), and if you quit with ExitThread, those resources will not be released and cause memory leaks. Although Delphi provides endthread (its internal call exitthread), this does not require us to manually operate (if manual operation is also a very troublesome thing, because many times you do not know when the thread is completed).

In addition to CreateThread, there is also a createremotethread, can build threads in other processes, this should not be the focus of learning now;

Now concentrate on CreateThread the parameters thoroughly.

Come on, let's talk about CreateThread. The thread handle that will be returned.

A "handle" is similar to a pointer, but a pointer is used to read and write an object, and the handle simply uses the object;

Objects with handles are generally system-level objects (or kernel objects); We are given a handle instead of a pointer, with only one purpose: "Security";

Seemingly through a handle can do a lot of things, but generally put the handle to a function (usually the system function), we will be more difficult to learn more; Actually, the system doesn't believe us.

Whether it's a pointer or a handle, it's just a small piece of data in memory (generally with a structure description), Microsoft doesn't disclose the structure details of the handle, guess what it should include: the real pointer address, access permission settings, reference count, and so on.

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.