Boost::this_thread::get_id() 不带字符串操作

发布于 2024-12-12 11:44:37 字数 223 浏览 0 评论 0原文

我所处的情况是,我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2024-12-19 11:44:37

无论如何,你的问题似乎完全基于误解。如果要将 boost::thread::id 放入变量中,该变量的类型应为 boost::thread::id。就像这样:

boost::thread::id MyVariable = boost::thread::get_id();

根本不涉及任何字符串。为什么要尝试将其转换为 char *

如果您的线程需要如此频繁地获取它们的 ID,从而造成瓶颈,那么您可能会做一些严重错误的事情。为什么这么需要一个线程的ID?

更新:好的,所以您需要一个具有特定语义的线程 ID。您需要分配具有所需语义的线程ID。 (不保证线程已经拥有的任何 ID 都可以在文件名中使用。)在伪代码中:

  1. 调用 get_id。
  2. 在地图中查找您检索到的 ID。
  3. 如果您找到此 ID 的条目,则返回其值,就完成了。
  4. 该线程没有可在文件名中使用的 ID。所以给它分配一个。存储您从 get_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 type boost::thread::id. Like this:

boost::thread::id MyVariable = boost::thread::get_id();

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:

  1. Call get_id.
  2. Look up the ID you retrieved in a map.
  3. If you found an entry for this ID, return its value, you're done.
  4. This thread has no ID that can be used in a file name. So assign it one. Store the pair of the ID you got from get_id and the ID you just assigned in the map. This will ensure the next time you try to get an entry for this thread, you will get the same one.

Alternatively, your platform may have a function that provides the semantics you need. For example, Linux has gettid and NT has GetCurrentThreadId.

旧时浪漫 2024-12-19 11:44:37

怎么样:

std::ostringstream oss;
oss << boost::thread::get_id();
std::string idAsString = oss.str();

请参阅 boost::thread::id

更新:
既然您已经使用了 boost,为什么不使用:

std::string id = boost::lexical_cast<std::string>(boost::thread::get_id());

另外,因为您仅在开始时执行此操作,速度应该不是问题。

What about:

std::ostringstream oss;
oss << boost::thread::get_id();
std::string idAsString = oss.str();

See the documentation on boost::thread::id

Update:
Since you already use boost, why not use:

std::string id = boost::lexical_cast<std::string>(boost::thread::get_id());

Also, as you are doing this only at the beginning speed should not be an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文