NameThreadForDebugging -- Naming threads for debugging
http://forums.devart.com/viewtopic.php?t=16907
type
tagTHREADNAME_INFO = record
dwType : LongWord; // Must be 0x1000.
szName : PAnsiChar; // Pointer to name (in user addr space).
dwThreadId : LongWord; // Thread ID (-=caller thread).
dwFlags : LongWord; // Reserved for future use, must be zero.
end; procedure NameThreadForDebugging (threadId: dword; threadName: AnsiString);
const
MS_VC_EXCEPTION = $406D1388;
{$ifdef VER7P}
var tni : tagTHREADNAME_INFO;
{$endif}
begin
{$ifdef VER7P}
if (threadName <> '') and (DebugHook <> ) then begin
tni.dwType := $;
tni.szName := PAnsiChar(threadName);
tni.dwThreadID := threadID;
tni.dwFlags := ;
try
RaiseException(MS_VC_EXCEPTION, , SizeOf(tni) div SizeOf(LongWord), @tni);
except end;
end;
{$endif}
end;
NameThreadForDebugging($FFFFFFFF, 'DBMonitor send thread')
http://stackoverflow.com/questions/23343765/best-practice-for-naming-a-thread
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.
What was wrong with my code?
The
NameThreadForDebugging
method is actually a static method. The second parameterThreadId
is optional and it equals the current thread id by default.
If I do not give aThreadId
clearly, I might very possibly name a the current thread, not the thread I really want to name it.
What is the solution?
Call
MyThread.NameThreadForDebugging('a_name', MyThread.ThreadId);
anywhere or CallNameThreadForDebugging('a_name');
at the beginning ofTMyThread.Execute
.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.
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(-)); 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;
procedure TMainForm.FormCreate( Sender : TObject );
begin
TThread.CurrentThread.NameThreadForDebugging( 'Main' );
TThread.NameThreadForDebugging( 'Main', MainThreadID );
TThread.NameThreadForDebugging( 'Main' );
end;
NameThreadForDebugging -- Naming threads for debugging的更多相关文章
- Threading in C#
http://www.albahari.com/threading/ PART 1: GETTING STARTED Introduction and Concepts C# supports par ...
- How to Analyze Java Thread Dumps--reference
原文地址:http://architects.dzone.com/articles/how-analyze-java-thread-dumps The Performance Zone is pres ...
- 不完全翻译:Threading in C#-Getting Started
Introduction(引入,介绍) and Concepts(概念) 原文地址:http://www.albahari.com/threading/ 注:水平有限不能全文翻译,备注了个别字段和短句 ...
- How to Analyze Java Thread Dumps
When there is an obstacle, or when a Java based Web application is running much slower than expected ...
- Rock the Tech Interview
Today, Infusion held a talk in Columbia University about tech interview. Talker: Nishit Shah @ Infus ...
- Visual Studio 2010初学者的调试指南:Mastering Debugging in Visual Studio 2010 - A Beginner's Guide
Introduction In the software development life cycle, testing and defect fixing take more time than a ...
- SOS.dll (SOS Debugging Extension)
SOS.dll (SOS Debugging Extension) lays threads associated with a live thread. The -special option di ...
- Debugging Chromium on Windows
转自:https://www.chromium.org/developers/how-tos/debugging-on-windows For Developers > How-Tos & ...
- Debugging JTAG Connectivity Problems
2013-12-04 22:34:26 转自:http://processors.wiki.ti.com/index.php/Debugging_JTAG_Connectivity_Problems ...
随机推荐
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.1创建虚拟机
2.1.创建虚拟机 2.1.1. 创建虚拟机节点1 2.1.2. 创建虚拟机节点2 操作如节点1. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境所有链 ...
- php生成 Arduino 12864 汉字内码
$ch = "你"; $ch = iconv("UTF-8","GB2312",$ch); $xx= sprintf("%X&qu ...
- JVM内存结构之三--持久代
本文会介绍一些JVM内存结构的基本概念,然后很快会讲到持久代,来看下Java SE 8发布后它究竟到哪去了. 基础知识 JVM只不过是运行在你系统上的另一个进程而已,这一切的魔法始于一个java命令. ...
- MySQL修改root密码的几种方法
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...
- 分段统计与Oracle的分析函数、逻辑判断等知识点的综合运用
重点部分:TOTAL层 项目要求: 统计每个巡检员(USER_ID)当前月的签到率及查询相关字段 签到率公式:以巡检员为单位, (当月至今天为止签到的所有点/该月巡检点的总个数)=(b.Point/a ...
- TopFreeTheme精选免费模板【20130703】
今天我们给大家分享13个最新的主题模板,5款WordPress主题,5款Joomla模板,3款OpenCart主题. BowThemes – BT Folio v1.0 Template for Jo ...
- (转)QR二维码生成及原理
二维码又称QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型:比如:字符,数字, ...
- iOS 检测有没有安装其它应用 和ios9下要注意的地方
UIApplication *app = [UIApplication sharedApplication]; NSURL *url = [NSURL URLWithString:@"Tri ...
- RxCache 的代码分析,含缓存时间duration的在代码中改变的自己实现的机制
当应用进程创建 RxCache 的实例后,会给应用进程返回一个 rxcache实例及一个 ProxyProvider,代码如下: CacheProviders providers = new RxCa ...
- 第二百零二天 how can I 坚持
最近增肥好明显,胃口好没办法,只要肚子起不来就行了.加油. 其实挺幸福,想吃啥吃啥. 鱼会不会被冻死,买了加热棒不想用,该咋办呢. 股市又跌没了一千多,还是不够睿智,不够淡定. 人活这一辈子,到底最想 ...