1

我有一个像这样的单位

type
 TMyClass = Class(TObject)
private
 AnInteger      : Integer;
 MyThreadHandle : DWORD;
 procedure MyPrivateProcedure;
public
 procedure MyPublicProcedure;
end;

procedure TMyClass.MyPrivateProcedure;
 procedure MyThread; stdcall;
 begin
  if AnInteger <> 0 then MyPublicProcedure;
 end;
var
 DummyID: DWORD;
begin
  MyThreadHandle := CreateThread(NIL,0,@MyThread,NIL,0, DummyID);
end;

procedure TMyClass.MyPublicProcedure;
begin
 AnInteger := 0;
end;

我的目标是拥有一个可以“访问”变量/函数/过程的线程(请不要使用 TTthread),就像它是类的一部分一样。此示例失败,因为它无权访问变量或过程。这只是一个例子,我知道 Integer 不能像那样改变。对我来说,有一个线程是课堂的一部分很重要。我还尝试将整数作为指针(有效)传递给线程,但我仍然无法访问该类的过程/函数。有任何想法吗?

4

2 回答 2

6

您可以使用 TThread 并保持文件大小较小。我认为你正在走上一条艰难的道路:重新发明轮子很费时间,我可以告诉你!:)

这是一些初始化线程的工作代码:

function ThreadProc(Thread: TThread): Integer;
var FreeThread: Boolean;
begin
  if not Thread.FTerminated then
  try
    result := 0; // default ExitCode
    try
      Thread.Execute;
    except
      on Exception do
        result := -1;
    end;
  finally
    FreeThread := Thread.FFreeOnTerminate;
    Thread.FFinished := True;
    if Assigned(Thread.OnTerminate) then
      Thread.OnTerminate(Thread);
    if FreeThread then
      Thread.Free;
    EndThread(result);   
  end;
end;

constructor TThread.Create(CreateSuspended: Boolean);
begin
  IsMultiThread := true; // for FastMM4 locking, e.g.
  inherited Create;
  FSuspended := CreateSuspended;
  FCreateSuspended := CreateSuspended;
  FHandle := BeginThread(nil, 0, @ThreadProc, Pointer(Self), CREATE_SUSPENDED, FThreadID);
  if FHandle = 0 then
    raise Exception.Create(SysErrorMessage(GetLastError));
  SetThreadPriority(FHandle, THREAD_PRIORITY_NORMAL); 
end;

也就是说,您将对象作为pointer()线程创建 API 传递,它将作为 ThreadProc 的唯一参数传递。

ThreadProc 不应该是任何方法的一部分,而是该单元的全局。

这是另一段代码直接调用 API 来处理多线程压缩,没有开销,并且同步:

type
  TThreadParams = record
    bIn, bOut: pAESBlock;
    BlockCount: integer;
    Encrypt: boolean;
    ID: DWORD;
    AES: TAES;
  end;

{ we use direct Windows threads, since we don't need any exception handling
  nor memory usage inside the Thread handler
   -> avoid classes.TThread and system.BeginThread() use
   -> application is still "officialy" mono-threaded (i.e. IsMultiThread=false),
     for faster System.pas and FastMM4 (no locking)
   -> code is even shorter then original one using TThread }
function ThreadWrapper(var P: TThreadParams): Integer; stdcall;
begin
  with P do
    AES.DoBlocks(bIn,bOut,bIn,bOut,BlockCount,Encrypt);
  ExitThread(0);
  result := 0; // make the compiler happy, but won't never be called
end;

procedure TAES.DoBlocksThread(var bIn, bOut: PAESBlock; Count: integer; doEncrypt: boolean);
var Thread: array[0..3] of TThreadParams; // faster than dynamic array
    Handle: array[0..3] of THandle; // high(Thread) is not compiled by XE2
    nThread, i, nOne: integer;
    pIn, pOut: PAESBlock;
begin
  if Count=0 then exit;
  if {$ifdef USEPADLOCK} padlock_available or {$endif}
    (SystemInfo.dwNumberOfProcessors<=1) or // (DebugHook<>0) or
    (Count<((512*1024) div AESBlockSize)) then begin // not needed below 512 KB
    DoBlocks(bIn,bOut,bIn,bOut,Count,doEncrypt);
    exit;
  end;
  nThread := SystemInfo.dwNumberOfProcessors;
  if nThread>length(Thread) then // a quad-core is enough ;)
    nThread := length(Thread);
  nOne := Count div nThread;
  pIn := bIn;
  pOut := bOut;
  for i := 0 to nThread-1 do
  with Thread[i] do begin // create threads parameters
    bIn := pIn;
    bOut := pOut;
    BlockCount := nOne;
    Encrypt := doEncrypt;
    AES := self; // local copy of the AES context for every thread
    Handle[i] := CreateThread(nil,0,@ThreadWrapper,@Thread[i],0,ID);
    inc(pIn,nOne);
    inc(pOut,nOne);
    dec(Count,nOne);
  end;
  if Count>0 then
    DoBlocks(pIn,pOut,pIn,pOut,Count,doEncrypt); // remaining blocks
  inc(Count,nOne*nThread);
  assert(integer(pIn)-integer(bIn)=Count*AESBlockSize);
  assert(integer(pOut)-integer(bOut)=Count*AESBlockSize);
  bIn := pIn;
  bOut := pOut;
  WaitForMultipleObjects(nThread,@Handle[0],True,INFINITE); 
  for i := 0 to nThread-1 do
    CloseHandle(Handle[i]);
end;
于 2012-06-07T09:40:44.087 回答
5

线程有自己的堆栈指针,因此您无法在MyThread本地过程中访问局部变量或参数(如隐藏的 Self 参数)(顺便说一句,这被声明为错误的)。此外,如果线程从外部函数访问变量(包括 Self),则不能对线程使用本地过程。而且如果你以后想使用64位编译器,你不能使用本地过程进行任何回调。

在您的情况下,您只需修复程序的声明并将其移动到单元范围(使其成为“独立”程序。这允许您使用线程回调参数“Self”。

function MyThread(MyObj: TMyClass): DWORD; stdcall;
begin
  if MyObj.AnInteger <> 0 then
    MyObj.MyPublicProcedure;
  Result := 0;
end;

procedure TMyClass.MyPrivateProcedure;
var
  DummyID: DWORD;
begin
  MyThreadHandle := CreateThread(nil, 0, @MyThread, Self, 0, DummyID);
end;
于 2012-06-07T09:40:59.650 回答