全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1340
推到 Plurk!
推到 Facebook!

有關使用multi thread計算的觀念問題

缺席
macchen
初階會員


發表:66
回覆:102
積分:33
註冊:2006-07-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-03-16 13:42:16 IP:219.87.xxx.xxx 訂閱
請問我將計算試使用2個thread分開計算後,要再將值相加,但是我沒使用vcl,但為何有沒有使用synchronize卻會造成值的計算錯誤,有用synchronize才會正常,沒用算出來的值卻會錯誤,並且用button3來跑迴圈,就是執行上面那二個thread的計算,因為觀念問題想問清楚,麻煩各位指點小弟一下,謝謝。

[code delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

const
testcount = 10999999;
const
WM_END = WM_USER 12;


type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Memo2: TMemo;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
FThreadCount: Integer;
d: tdatetime;
protected
public
{ Public declarations }
count: integer;
st: TDateTime;
single: Boolean;
procedure TerminalThread(Sender: TObject);
procedure StartThread(CPUNo: integer = 0; Assign: Boolean = True);
procedure TEnd(var msg: TMessage); message WM_END;
procedure EndThread;
end;
Mythread = class(TThread)
protected
value:Integer;
procedure DoCount();
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
gcount:Int64;
{ Mythread }
procedure Mythread.DoCount;
var
i, j: Integer;
begin
for i := 1 to testcount do
j := i * i;
end;
procedure Mythread.Execute;
var
i: Integer;
begin
for i := 0 to 100 do
docount;
//Synchronize(docount) ;
sendmessage(form1.Handle,WM_END,0,0);
end;
{ TForm1 }
procedure TForm1.TerminalThread;
begin
Dec(FThreadCount);
if FThreadCount = 0 then
//label1.Caption := inttostr(gcount) ' ' formatdatetime('ss.zzz', now-d);
label1.Caption := formatdatetime('ss.zzz', now - d);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
c: int64;
begin
c := 0;
d := now;
for i := 0 to 200 do
for j := 1 to testcount do
c := j* j;
label2.Caption := formatdatetime('ss.zzz', now - d);
end;
procedure TForm1.StartThread(CPUNo: integer; Assign: Boolean);
const
CPU_Mask: array[0..31] of DWORD = (
$1, $2, $4, $8,
$10, $20, $40, $80,
$100, $200, $400, $800,
$1000, $2000, $4000, $8000,
$10000, $20000, $40000, $80000,
$100000, $200000, $400000, $800000,
$1000000, $2000000, $4000000, $8000000,
$10000000, $20000000, $40000000, $80000000);
begin
with Mythread.Create(True) do
begin
FreeOnTerminate := True;
//OnTerminate := TerminalThread;
Resume;
if Assign then
SetThreadAffinityMask(Handle, CPU_Mask[CPUNo]);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
m1, m2: mythread;
i: Integer;
begin
gcount := 0 ;
FThreadCount := 2;
Count := 1 ;
d := now;
st := now;
m1 := Mythread.Create(false);
//m1.OnTerminate := TerminalThread;
m2 := Mythread.Create(false);
//m2.OnTerminate := TerminalThread;
end;
procedure TForm1.EndThread;
var
S: string;
begin
if Single then S:= '單線程 ' else S:= '2線程 ';
Memo1.Lines.Add(Format(
'%s,執行第 %d 次,時間 %f 秒 ',[S,Count,3600 * 24 * (Now - St)]));
end;
procedure TForm1.TEnd(var msg: TMessage);
begin
EndThread;
if Single then
begin
if Count < 2 then
begin
Inc(Count);
StartThread;
end;
end
else
begin
if Count < 2 then
begin
Inc(Count);
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Count := 1 ;
FThreadCount := 2;
Single := False;
d := now;
st := now;
StartThread(0);
StartThread(1);
end;
end.

[/code]
------
DELPHI初學者
系統時間:2024-11-21 19:47:00
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!