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

查看:50
本文介绍了如何将名称设置为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.

干杯!

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

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天全站免登陆