Function createthread (usage: pointer; dwstacksize: DWORD; {stack size} lpstartaddress: tfnthreadstartroutine; lpparameter: pointer; dwcreationflags: DWORD; var lpthreadid: DWORD): thandle; stdcall;
The second parameter of createthread is the size of the stack allocated to the thread.
First, we can know that each thread has its own stack (and its own Message Queue ).
What is a stack? In fact, stack is a stack, stack is a stack, and sometimes "stack" is also called "stack ".
They are all memory areas in the process, mainly in different access methods (Stack: first-in-first-out; heap: first-in-first-out );
"Stack" (or stack) is suitable for accessing temporary and lightweight variables and is mainly used to store local variables. For example, I in for I: = 0 to 99 DO can only be stored in the stack, you cannot use a global variable for the For Loop count.
Now we know that the thread has its own "stack", and the stack size can be allocated when the thread is created.
In all the preceding examples, the value is 0, which indicates that the default size of the system is used. The default size is the same as that of the main thread stack. If it is not enough, it will automatically increase;
How big is the stack of the main thread? This value can be set: Project-> options-> Delphi compiler-> linking ()
The stack is private, but the stack is public. If different threads use a global variable, it is a bit messy;
To solve this problem, Delphi provides us with a threadvar keyword similar to var. When a thread uses the global variable declared by threadvar, it will leave a copy in its respective stack, which resolves the conflict. however, we recommend that you use local variables as much as possible or use the class member variables when inheriting tthread. Because threadvar is inefficient, it is said that it is 10 times slower than local variables.
In the following example, the differences between variables defined using var and threadvar are tested.
Use VaR:
Use threadvar:
Code File:
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} // var num: integer; {global variable} threadvar num: integer; {global variable supporting multithreading} function mythreadfun (P: pointer): DWORD; stdcall; var py: integer; begin py: = INTEGER (p); While true do begin Inc (Num); with form1.canvas do begin lock; textout (20, Py, inttostr (Num); unlock; end; sleep (1000); {the thread is suspended for 1 second and then continues} end; Procedure tform1.button1click (Sender: tobject); var ID: DWORD; the begin {borrow interface function parameter transmits the Y value in a coordinate point to let each thread output the result in different locations} createthread (nil, 0, @ mythreadfun, PTR (20), 0, ID); createthread (nil, 0, @ mythreadfun, PTR (40), 0, ID); createthread (nil, 0, @ mythreadfun, PTR (60), 0, ID); end.
Form file:
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 106 clientwidth = 180 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false pixelsperinch = 96 textheight = 13 object button1: tbutton left = 80 top = 40 width = 75 Height = 25 caption = 'button1' taborder = 0 onclick = button1click endend