[fpc-devel] OO rewrite - technical questions
Michael Schnell
mschnell at lumino.de
Mon Jul 26 11:13:21 CEST 2010
On 07/24/2010 05:12 PM, Hans-Peter Diettrich wrote:
>
> In the meantime I found one possible use for threadvars: when some
> subroutine can be called from different threads, it may want to
> retrieve the thread context, e.g. the thread object itself. Right?
Yep. But of course you can do multiple threads that completely execute
the same code (e.g. a Web Server that handles multiple externals
requests at the same time.
-Michael
Here a simple test program with threadvars;
unit Unit61;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, StdCtrls;
type
TForm61 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyThread = class(TThread)
procedure execute; override;
end;
var
Form61: TForm61;
test: Integer;
teststr: String;
MyThread1, MyThread2 : TMythread;
threadvar threadtest: Integer;
implementation
{$R *.dfm}
function dotest: String;
begin
inc(test);
inc(threadtest);
Result := IntToStr(test) + '/' + IntToStr(threadtest);
end;
procedure TForm61.Button1Click(Sender: TObject);
var s: String;
begin
s := dotest;
teststr := teststr + ' ' + s;
caption := teststr;
end;
procedure TForm61.FormCreate(Sender: TObject);
begin
test := 10;
threadtest := 20;
MyThread1 := TMythread.Create(false);
MyThread2 := TMythread.Create(false);
end;
{ TMyThread }
procedure TMyThread.execute;
var s: String;
begin
s:= dotest;
teststr := teststr + ' .. ' + s;
while (true) do
sleep(10000);
end;
end.
More information about the fpc-devel
mailing list