如何将名称设置为 Win32 线程? [英] How to set name to a Win32 Thread?

查看:22
本文介绍了如何将名称设置为 Win32 线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 Win32 线程设置名称.我没有找到任何 Win32 API 来实现相同的目标.基本上我想在日志文件中添加线程名称.TLS(线程本地存储)是唯一的方法吗?

How do I set a name to a Win32 thread. I did'nt find any Win32 API to achieve the same. Basically I want to add the Thread Name in the Log file. Is TLS (Thread Local Storage) the only way to do it?

推荐答案

您始终可以将这些信息存储在合适的数据结构中.使用哈希或映射将 GetThreadId() 映射到此名称.由于 GetThreadId() 始终是唯一标识符,因此它可以正常工作.

You can always store this information for yourself in a suitable data structure. Use a hash or a map to map GetThreadId() to this name. Since GetThreadId() is always a unique identifier, this works just fine.

干杯!

当然,如果他创造了很多线程,该哈希图将慢慢填充up 并使用越来越多的内存,所以一些清理程序可能是一个也是好事.

Of course, if he's creating many threads, that hashmap will slowly fill up and use more and more memory, so some cleanup procedure is probably a good thing as well.

你说得对.当一个线程死亡时,它在地图中的相应条目自然应该被删除.

You're absolutely right. When a thread dies, it's corresponding entry in the map should naturally be removed.

这篇关于如何将名称设置为 Win32 线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆