Multithreaded programming CreateThread (explained Tcontext)

Source: Internet
Author: User

[Delphi]View PlainCopy
  1. function CreateThread (
  2. Lpthreadattributes:pointer; {security Settings}
  3. Dwstacksize:dword; {stack size}
  4. Lpstartaddress:tfnthreadstartroutine; {entry Function}
  5. Lpparameter:pointer; {function Argument}
  6. Dwcreationflags:dword; {startup options}
  7. var lpthreadid:dword {output thread ID}
  8. ): Thandle;                     stdcall; {return thread handle}


--------------------------------------------------------------------------------

Creating a thread on Windows is inseparable from the CreateThread function;
Tthread.create is the first call to Beginthread (Delphi Custom), beginthread and call CreateThread.
Since there is a build, it should be released, CreateThread corresponding release function is: ExitThread, such as the following code:
--------------------------------------------------------------------------------

[Delphi]View PlainCopy
    1. Procedure TForm1.  Button1Click (Sender:tobject);
    2. Begin
    3. ExitThread (0); {This sentence exits the current program, but is not recommended for use}
    4. End


--------------------------------------------------------------------------------

Code Comment:
The current program is a process, the process is just a working environment, threads are workers;
Each process will have a boot thread (or the main thread), that is to say: we have a lot of code is written to the main thread;
Above the 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 other functions after the use of, here to give an unsigned integer can.

Perhaps you will say: This exitthread is very useful; In fact, whether it is using the API or the TThread class to write multi-threading, we seldom use it; Because:
1, if the direct use of API CreateThread, it executes the entry function will automatically exit, no need to exitthread;
2, the thread established with TThread class must not use ExitThread exit; Because when you use TThread to create a thread, you allocate more resources (such as your custom member, its ancestor class (TObject) allocated resources, and so on), and if you exit with ExitThread, the resources will not be released and the memory leaks. Although Delphi provides endthread (its internal call to ExitThread), this does not require us to do it manually (it is cumbersome to do this manually, since many times you do not know when the thread was executed).

--------------------------------------------------------------------------------

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

Come on backwards, let's talk about the "thread handle" that CreateThread will return.

A handle is like a pointer, but a pointer can read or write an object, and a handle simply uses the object;
Objects with handles are generally system-level objects (or kernel objects); The reason given to us is a handle rather than a pointer, with only one purpose: "Safety";
Seemingly through the handle can do a lot of things, but generally put the handle to a function (usually a system function), we also end up difficult to understand more; In fact, the system doesn't believe us.

Whether it is a pointer or a handle, is just a small piece of data in memory (general structure description), Microsoft does not expose the structure of the handle details, guess it should include: real pointer address, access permissions settings, reference count, and so on.

Since CreateThread can return a handle, the thread belongs to "kernel object".
In fact, what process does not belong to the pipeline, they are equal in the arms of the system; At the same time, the system will run each thread at the same interval, but the interval is so small that it makes us mistakenly assume that the program is running continuously.

At this point you should have a question: How does the system remember the data state of the previous thread when it executes other threads?
There is such a structure tcontext, it is basically a collection of CPU registers, the thread is the data is through this structure switch, we can also through the GetThreadContext function read register to see.

Attach the definition of this structure tcontext (or called: CONTEXT, _context):
--------------------------------------------------------------------------------

[Delphi]View PlainCopy
  1. PContext = ^tcontext;
  2. _context = Record
  3. Contextflags:dword;
  4. Dr0:dword;
  5. Dr1:dword;
  6. Dr2:dword;
  7. Dr3:dword;
  8. Dr6:dword;
  9. Dr7:dword;
  10. Floatsave:tfloatingsavearea;
  11. Seggs:dword;
  12. Segfs:dword;
  13. Seges:dword;
  14. Segds:dword;
  15. Edi:dword;
  16. Esi:dword;
  17. Ebx:dword;
  18. Edx:dword;
  19. Ecx:dword;
  20. Eax:dword;
  21. Ebp:dword;
  22. Eip:dword;
  23. Segcs:dword;
  24. Eflags:dword;
  25. Esp:dword;
  26. Segss:dword;
  27. End


--------------------------------------------------------------------------------

The last parameter of CreateThread is "thread ID";
Since you can return a handle, why do you want to export the ID? Now what I know is:
1, the ID of the thread is unique; The handle may be more than one, for example, you can use GetCurrentThread to get a pseudo-handle, you can copy a handle with DuplicateHandle, and so on.
2. ID is lighter than handle.

In the main thread, GetCurrentThreadID, Mainthreadid, maininstance get the ID of the main thread.

Transferred from: http://www.cnblogs.com/del/archive/2009/02/10/1387559.html

http://blog.csdn.net/rznice/article/details/8245788

Multithreaded programming CreateThread (explained Tcontext)

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.