多線程編程(4) - 從 CreateThread 說起[續(xù)二]。
function CreateThread(
lpThreadAttributes: Pointer;
dwStackSize: DWORD;
lpStartAddress: TFNThreadStartRoutine;
lpParameter: Pointer; {入口函數(shù)的參數(shù)}
dwCreationFlags: DWORD;
var lpThreadId: DWORD
): THandle; stdcall;
線程入口函數(shù)的參數(shù)是個(gè)無類型指針(Pointer), 用它可以指定任何數(shù)據(jù); 本例是把鼠標(biāo)點(diǎn)擊窗體的坐標(biāo)傳遞給線程的入口函數(shù), 每次點(diǎn)擊窗體都會(huì)創(chuàng)建一個(gè)線程.
運(yùn)行效果圖:
代碼文件:unit Unit1;
interface
uses
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}
var
pt: TPoint; {這個(gè)坐標(biāo)點(diǎn)將會(huì)已指針的方式傳遞給線程, 它應(yīng)該是全局的}
function MyThreadFun(p: Pointer): Integer; stdcall;
var
i: Integer;
pt2: TPoint; {因?yàn)橹羔槄?shù)給的點(diǎn)隨時(shí)都在變, 需用線程的局部變量存起來}
begin
pt2 := PPoint(p)^; {轉(zhuǎn)換}
for i := 0 to 1000000 do
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);
{下面這種寫法更好理解, 其實(shí)不必, 因?yàn)?PPoint 會(huì)自動(dòng)轉(zhuǎn)換為 Pointer 的}
//CreateThread(nil, 0, @MyThreadFun, Pointer(@pt), 0, ID);
end;
end.
窗體文件:object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 128
ClientWidth = 229
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnMouseUp = FormMouseUp
PixelsPerInch = 96
TextHeight = 13
end
這個(gè)例子還有不嚴(yán)謹(jǐn)?shù)牡胤? 當(dāng)一個(gè)線程 Lock 窗體的 Canvas 時(shí), 其他線程在等待; 線程在等待時(shí), 其中的計(jì)數(shù)也還在增加. 這也就是說: 現(xiàn)在并沒有去處理線程的同步; 同步是多線程中最重要的課題, 快到了.
另外有個(gè)小技巧: 線程函數(shù)的參數(shù)是個(gè) 32 位(4個(gè)字節(jié))的指針, 僅就本例來講, 可以讓它的 "高16位" 和 "低16位" 分別攜帶 X 和 Y; 這樣就不需要哪個(gè)全局的 pt 變量了.
其實(shí)在 Windows 的消息中就是這樣傳遞坐標(biāo)的, 在 Windows 的消息中一般高字節(jié)是 Y、低字節(jié)是 X; 咱們這么來吧, 這樣還可以使用給消息準(zhǔn)備的一些方便的函數(shù).
重寫本例代碼(當(dāng)然運(yùn)行效果和窗體文件都是一樣的):unit Unit1;
interface
uses
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;
x,y: Word;
begin
x := LoWord(Integer(p));
y := HiWord(Integer(p));
{如果不使用 LoWord、HiWord 函數(shù)可以像下面這樣: }
//x := Integer(p);
//y := Integer(p) shr 16;
for i := 0 to 1000000 do
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);
{如果不使用 MekeLong、MakeWParam、MakeLParam、MakeResult 等函數(shù), 可以像下面這樣: }
//num := Y shl 16 + X;
CreateThread(nil, 0, @MyThreadFun, Ptr(num), 0, ID);
{上面的 Ptr 是專門將一個(gè)數(shù)字轉(zhuǎn)換為指針的函數(shù), 當(dāng)然也可以這樣: }
//CreateThread(nil, 0, @MyThreadFun, Pointer(num), 0, ID);
end;
end.
聯(lián)系客服