multithreading - Best practice for naming a thread -
Recently, I worked on a high concurrent event operated framework (Java Akaka) threads When I debug an Acka app, the thread contains very meaningful names. It's really amazing when I return to Delphi, so I think all threads are unaware, though for the last 20 years, their name is already unknown.
For all of my designed thread sections, I follow a pattern of which I define a setter SetThreadName
and I NameThreadForDebugging code> in the
Execute
method. It works fine so far
type TMyThread = class (TThread) private FThreadName: string; Executed secure process; Override; Public Process SetThreadName (Const. Threadname: string); End; Process TMyThread.SetThreadName (const ThreadName: string); Start FThreadName: = ThreadName; End; TMyThread.Execute Process; Start NameThreadForDebugging (FThreadName); // End the normal thread execution code here;
But those instances of third-party threads will not be named, unless I am making a string thread square, Delphi Magic is SetThreadName
to set base thread in the class? I can use the Detour.pas
to call the NameThreadForDebugging (FThreadName)
in the first place of the Execute
method.
Any ideas?
Update 1 Thanksgiving, please help them to help other readers better understand my question, the question is obviously again : Has been reserved.
-
What was wrong with my code?
NameThreadForDebugging
method is actually a static method. The second parameterthread ID
is optional and it equals current thread ID by default if I explicitly I do not want to name the current thread, no thread, I really want to name it. -
Call
MyThread.NameThreadForDebugging ('a_name', MyThread.ThreadId);
anywhere or callNameThreadForDebugging ('a_name'); In the beginning of
TMyThread.Execute
. -
Why is it confusing to make things right?
I do not understand why not provide a non-stable version without second
thread ID
if this was such a non-static version, then I did not make this mistake Could
I am extemporising here but albeit as though it It sounds to me that you believe it is possible to designate the thread with the code executed within that thread. But that is not the case. In order to name a thread, all you need is your ID
The function gives signatures so much:
class process NameThreadForDebugging (athreadName: AnsiString ; AThreadID: TThreadID = TThreadID (-1)); Stable;
If you are not giving optional thread ID parameters, then -1
is passed which is interpreted as meaning, the thread being executed . How are you using NameThreadForDebugging
However, you can simply pass the thread ID because you explicitly put thread examples, you also have their IDs.
An example method of the thread that is known by the thread in the interface you thought is called. You are assuming to write this code:
Thread .SetThreadName (ThreadName);
Instead of doing this, you can just type:
TThread.NameThreadForDebugging (ThreadName, Thread.ThreadID);
If you want to use a class assistant then you can do this like this:
type TThreadHelper = TThread public process for SetThreadName (constant ThreadName Class assistant: string); End; Process TThreadHelper.SetThreadName (Const. Threadname: string); Start TThread.NameThreadForDebugging (ThreadName, ThreadID); End;
Frankly, I do not think I will go into such a problem. Calling NameThreadForDebugging
seems perfectly enough.
Comments
Post a Comment