Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for naming a thread

Recently I worked on a high concurrent event-driven framework (Java Akka), which will create massive Actor threads. When I debug an Akka application, threads have very meaningful names. It'd really awesome. When I switch back to Delphi, I feel upset that all threads are unnamed, although they are unnamed for the past 20 years already.

For all my own designed thread classes, I follow such a pattern that I define a setter SetThreadName and I call NameThreadForDebugging in the Execute method. This works fine so far.

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;

But those instances of 3rd-party threads will stay unnamed, unless I create a descent thread class. Is there Delphi Magic to set SetThreadName to the base Thread class? I can use Detour.pas to force NameThreadForDebugging(FThreadName) be called at the first place of Execute method.

Any ideas?


Update 1 Thanks David for his kindly help. To help other readers to understand my question well, the question has been sightly rephrased.

  1. What was wrong with my code?

    The NameThreadForDebugging method is actually a static method. The second parameter ThreadId is optional and it equals the current thread id by default. If I do not give a ThreadId clearly, I might very possibly name a the current thread, not the thread I really want to name it.

  2. What is the solution?

    Call MyThread.NameThreadForDebugging('a_name', MyThread.ThreadId); anywhere or Call NameThreadForDebugging('a_name'); at the beginning of TMyThread.Execute.

  3. Why so confused to make things right?

    I do not understand, why not provide a non-static version without the second ThreadId. If there was such a non-static version, I could not have made this mistake.

like image 647
stanleyxu2005 Avatar asked Mar 20 '23 18:03

stanleyxu2005


1 Answers

I'm extemporising here, but it looks to me as though you believe that it is only possible to name a thread from code executing inside that thread. But that is not the case. In order to name a thread all you need is its ID.

The documentation gives the function signature as so:

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

If you don't supply the optional thread ID parameter then -1 is passed which is interpreted as meaning, the executing thread. Which is how you have been using NameThreadForDebugging thus far. However, you can just pass the thread ID. Since you clearly have thread instances, you also have their IDs at hand.

The interface that you have imagined involves calling an instance method of the thread passing the name of the thread. That is you imagine writing this code:

Thread.SetThreadName(ThreadName);

Instead of doing that, you can simply write:

TThread.NameThreadForDebugging(ThreadName, Thread.ThreadID);

If you want to use a class helper you can do it like this:

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;

Frankly, I don't think I would go to such trouble. Calling NameThreadForDebugging seems perfectly adequate.

like image 187
David Heffernan Avatar answered Mar 22 '23 23:03

David Heffernan