“主”的ID线程在c ++ [英] id of "main" thread in c++

查看:97
本文介绍了“主”的ID线程在c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在c ++获得主程序线程的ID?

Is there a way in c++ to get the id of the "main" program thread?

我看到 std :: this_thread: :get_id()获取当前正在执行的线程的id,但我需要原始程序线程 main 的id。我没有看到任何函数来获得这个。

I see that std::this_thread::get_id() gets the id of the currently executing thread but I need the id of the main, original program thread. I don't see any function to get this.

原因是我有一些非线程安全的内部函数,只能在应用程序的原始线程上调用所以要安全我想做: -

The reason is that I have some non thread safe internal functions that must only be called on the original thread of the application so to be safe I want to do :-

assert(std::this_thread::get_id() == std::main_thread::get_id());

但是当然不是一个函数,我看不到任何方式

But there of course isn't a function to do that, and I can't see any way to get that information.

推荐答案

您可以保存它,而 this_thread 仍然是原始的线程:

You could save it while this_thread is still the original thread:

std::thread::id main_thread_id;

int main() {
    main_thread_id = std::this_thread::get_id(); // gotcha!
    /* go on */
}

这篇关于“主”的ID线程在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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