Best practice for naming threads

I recently worked on a high event-driven concurrent platform (Java Akka) that will create massive threads Actor. When I debug an Akka application, threads have very meaningful names. It would be great. When I return to Delphi, I am upset that all the streams are unnamed, although they are unnamed for 20 years.

For all my own stream classes, I follow the pattern that I define setter SetThreadNameand call NameThreadForDebuggingin the method Execute. So far this is wonderful.

type
  TMyThread = class(TThread)
  private
    FThreadName: string;
  protected
    procedure Execute; override;
  public
    procedure SetThreadName(const ThreadName: string);
  end;

procedure TMyThread.SetThreadName(const ThreadName: string);
begin
  FThreadName := ThreadName;
end;

procedure TMyThread.Execute;
begin
  NameThreadForDebugging(FThreadName);
  // Put normal thread execution code here
end;

, . Delphi Magic, SetThreadName Thread? Detour.pas NameThreadForDebugging(FThreadName) Execute.

?


1 . , .

  • ?

    NameThreadForDebugging . ThreadId . ThreadId , , , , .

  • ?

    MyThread.NameThreadForDebugging('a_name', MyThread.ThreadId); NameThreadForDebugging('a_name'); TMyThread.Execute.

  • , ?

    , ThreadId. , .

+4
1

extemporising, , , , . . , .

:

class procedure NameThreadForDebugging(AThreadName: AnsiString; 
  AThreadID: TThreadID = TThreadID(-1)); static;

, -1, , . NameThreadForDebugging. . , .

, , , . :

Thread.SetThreadName(ThreadName);

:

TThread.NameThreadForDebugging(ThreadName, Thread.ThreadID);

, :

type
  TThreadHelper = class helper for TThread
  public
    procedure SetThreadName(const ThreadName: string);
  end;

procedure TThreadHelper.SetThreadName(const ThreadName: string);
begin
  TThread.NameThreadForDebugging(ThreadName, ThreadID);
end;

, , . NameThreadForDebugging .

+7

Source: https://habr.com/ru/post/1538423/


All Articles