Multithreaded Programming (4)--from CreateThread (the third parameter to the bottom)

Source: Internet
Author: User
Tags textout

Reprinted from: In Case of blog

function CreateThread (    lpthreadattributes:pointer;    Dwstacksize:dword;    Lpstartaddress:tfnthreadstartroutine;    Lpparameter:pointer;    {Parameters of the entry function}    Dwcreationflags:dword;    var Lpthreadid; DWORD;): Thandle; stdcall;

    

  the argument to the thread entry function is an untyped pointer (Pointer), which allows you to specify any data (which is one of the powerful pointers); This example is the entry function that passes the coordinates of the mouse click on the form to the thread, and creates a thread each time a click on the form, running the following

The code is as follows

Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs;type TF Orm1=class (Tform) procedure formmouseup (sender:tobject;            Button:tmousebutton; Shift:tshiftstate;    X, Y:integer);    End;var form1:tform1;implementation{$R *.dfm}var pt:tpoint; This coordinate point is passed to the thread as a pointer, and it should be the global function Mythreadfun (p:pointer): Integer;    Stdcall;var I:integer;        Pt2:tpoint;    Since the points given by the pointer parameters are changing at any time, the local variables of the thread need to be stored up begin pt2:= PPoint (p) ^;            Convert for i:=0 to 1000000 does begin with Form1.canvas do begin Lock; TextOut (pt2. X, Pt2.            Y, IntToStr (i));        UnLock;    End    End result:= 0;end;procedure tform1.formmouseup (sender:tobject;    Button:tmousebutton; Shift:tshiftstate;    x, Y:integer); var id:dword;begin pt:= Point (x, Y);    CreateThread (nil, 0, @MyThreadFun, @pt, 0, ID); The following wording is better understood, in fact, because PPoint will automatically convert to Pointer's//createthread (nil, 0, @MyThreadFun, Pointer (@pT), 0, ID); End;end. 

    

Form files

Object Form1:tform1 Left  = 0  Top = 0  Caption = ' Form1 '  clientheight =  clientwidth = 229  Color = Clbtnface  font.charset = default_charset  font.color = clwindowtext  font.height = -11  font.name = ' Tahoma '  font.style = []  oldcreateorder = False  OnMouseUp = formmouseup  pixelsperinch =  TextHeight = 13end

    

This example is not rigorous: when a thread lock form is canvas, the other threads are waiting, and the threads are waiting, and the technology is increasing. This is to say: Now does not deal with thread synchronization, synchronization is the most important topic in multi-threading, it is almost

Another tip: The thread function parameter is a 32-bit (4-byte) pointer, and for this example, it can carry X and y respectively for its "high 16-bit" and "low 16-bit", so that the global PT variable is not needed.

In fact, in Windows message that is the way to pass the coordinates, in the Windows message generally high-byte is Y, low byte is x; let's do this so we can use some handy functions for message preparation.

Override This example code (of course the run effect is the same as the form file):

Unit Unit1;interface Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs;type tform1= Class (Tform) procedure formmouseup (sender:tobject; Button:tmousebutton; Shift:tshiftstate;    X, Y:integer); End;var form1:tform1;implementation{$R *.dfm}function mythreadfun (p:pointer): Integer;            Stdcall;var I:integer;            Integer is 32bit,4 bytes x, Y:word;    Word is a 16bit,2 byte begin x:= LoWord (Integer (p));    Converts p to integer and obtains its low-byte y:= hiword (Integer (p));    Similar to the above {if you do not use the LoWord, HiWord function, you can do the following:}//x:= Integer (p);        y:= Integer (p) shr 16;            For i:= 0 to 1000000 does begin with Form1.canvas do begin Lock;            TextOut (x, Y, IntToStr (i));        UnLock;    End    End result:= 0;end;procedure tform1.formmouseup (sender:tobject;    Button:tmousebutton; Shift:tshiftstate;    X, Y:integer); var Id:dword;    Num:integer;begin num:= Makelong (X, Y); {If you do not use Makelong, MAKEWPAFunctions such as RAM, Makelparam, Makeresult, etc. can be as follows:}//num:= Y SHL +x;    CreateThread (nil, 0, @MuThreadFun, Ptr (num), 0, ID); {Above Ptr is a function that converts a number to a pointer, and of course it can}//createthread (nil, 0, @MyThreadFun, Pointer (num), 0, ID); End;end.

   

Multithreaded Programming (4)--from CreateThread (the third parameter to the bottom)

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.