Multithreaded Programming (6)-From CreateThread [Cont.]

Source: Internet
Author: User

function CreateThread(
 lpThreadAttributes: Pointer;
 dwStackSize: DWORD; {堆栈大小}
 lpStartAddress: TFNThreadStartRoutine;
 lpParameter: Pointer;
 dwCreationFlags: DWORD;
 var lpThreadId: DWORD
): THandle; stdcall;

The second parameter of the CreateThread is the stack size assigned to the thread.

This first of all lets us know that each thread has its own stack (and also its own message queue).

What is a stack? In fact heap is heap, stack is stack, sometimes "stack" also is called "stack".

They are memory areas in the process, mainly in the way of access (stack: Advanced Out; heap: FIFO first);

"Stack" (or stack) is suitable for accessing temporary and lightweight variables, mainly for storing local variables; For example, for I: = 0 I can only be stored on the stack, you can not use a global variable for the loop count.

Now we know that threads have their own "stacks" and can allocate the size of the stack when creating threads.

In all previous examples, this value is 0, which means that the default size of the system is the same size as the main line stacks, which automatically grows if not enough;

How big is the stack of the main thread? This value can be set: Project-> Options-> Delphi Compiler-> linking (pictured)

Stacks are private, but the heap is public, and if different threads are using a global variable a bit messy;

To solve this problem Delphi provides us with a threadvar keyword similar to var, where threads leave a copy of their stack using a global variable declared by threadvar, which resolves the conflict. However, use local variables as much as possible, or use the class's member variables when inheriting TThread because Threadvar is not efficient and is said to be 10 times times slower than local variables.

The following example tests the difference between defining variables with Var and threadvar.

To use the Var effect chart:

Using the Threadvar Effect chart:

Code files:

Unit Unit1
Interface
uses
Windows, Messages, sysutils, 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; {Support for multithreaded global variables}
Function Mythreadfun (p:pointer): DWORD; stdcall;  
var
py:integer
Begin
Py: = Integer (p);
  While True does
begin
Inc (num);
   With Form1.canvas do begin
Lock;
TextOut (inttostr, py, num);
Unlock;
End;
Sleep (1000); {The thread hangs for 1 seconds and then continues}
End;
End;
Procedure Tform1.button1click (sender:tobject);
var
id:dword
Begin
{The parameters of the borrow-entry function pass the Y value in a coordinate point so that each thread outputs the result in a different position}
CreateThread (nil, 0, @MyThreadFun, PTR (0, ID);
CreateThread (nil, 0, @MyThreadFun, Ptr (), 0, ID);
CreateThread (nil, 0, @mYthreadfun, Ptr (0, ID);
End;
End.

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.