id of “main” thread in c++

前端 未结 2 973
灰色年华
灰色年华 2020-12-17 23:09

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

I see that std::this_thread::get_id() gets the id of the currently executing thread

相关标签:
2条回答
  • 2020-12-17 23:47

    This topic seems to be discussed quite a few times here, like in this topic:

    • Getting a handle to the process main thread

    You can find some solutions, but I'd just think the other way round... When starting the new threads, just supply the id of the main thread to them, and store that in a field in the other threads. If that is not going to change throughout the threads' life, you're good to go, you can reference the "main" thread by those handles.

    0 讨论(0)
  • 2020-12-17 23:49

    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 */
    }
    
    0 讨论(0)
提交回复
热议问题