Boost::this_thread::get_id() 不带字符串操作
我所处的情况是,我需要将 boost::thread::id 放入变量中,但该变量不是字符串,也不允许进行字符串操作。这可能吗?
也许类似于
char *tid = (casting some) boost::this_thread::get_id()
这是 UNIX 系统上的 C++。 之所以避免使用字符串操作是因为它会减慢整个应用程序的速度。谢谢之前。
i'm in a situation where i need to get boost::thread::id into a variable, but this variable is NOT a string, neither a string operation is allowed. is that possible?
maybe something like
char *tid = (casting something) boost::this_thread::get_id()
this is c++ on unix system.
the reason why avoiding using string operation is because it slows down the whole application speed. thanks before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论如何,你的问题似乎完全基于误解。如果要将
boost::thread::id
放入变量中,该变量的类型应为boost::thread::id
。就像这样:根本不涉及任何字符串。为什么要尝试将其转换为
char *
?如果您的线程需要如此频繁地获取它们的 ID,从而造成瓶颈,那么您可能会做一些严重错误的事情。为什么这么需要一个线程的ID?
更新:好的,所以您需要一个具有特定语义的线程 ID。您需要分配具有所需语义的线程ID。 (不保证线程已经拥有的任何 ID 都可以在文件名中使用。)在伪代码中:
或者,您的平台可能具有提供您需要的语义的功能。例如,Linux 有
gettid
,NT 有GetCurrentThreadId
。In any event, your question seems to be entirely based on a misconception. If you want to get the
boost::thread::id
into a variable, the variable should be of typeboost::thread::id
. Like this:No strings are involved at all. Why are you trying to cast it to a
char *
?If your threads need to get their IDs so often that it's creating a bottleneck, you are likely doing something horribly wrong. Why do you need a thread's ID so much?
Update: Okay, so you need a thread ID that has specific semantics. You need to assign threads IDs that have the semantics you require. (There is no guarantee that whatever ID the threads already have is usable in a file name.) In pseudo-code:
Alternatively, your platform may have a function that provides the semantics you need. For example, Linux has
gettid
and NT hasGetCurrentThreadId
.怎么样:
请参阅 boost::thread::id
更新:
既然您已经使用了 boost,为什么不使用:
另外,因为您仅在开始时执行此操作,速度应该不是问题。
What about:
See the documentation on boost::thread::id
Update:
Since you already use boost, why not use:
Also, as you are doing this only at the beginning speed should not be an issue.