C++ Logo

std-proposals

Advanced search

[std-proposals] std::thread::first_ever

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 30 Apr 2023 11:37:48 +0100
A computer program starts out as just one thread. It can spawn a
second thread and a third thread. Then the first thread can terminate,
leaving the second and third to continue.

When we say 'the main thread', 99% of the time we are talking about
the very first thread that the program started. In rare cases, if the
first thread has terminated, we might refer to the second thread as
'the main thread'. Or if the second has also terminated, maybe we'll
call the third thread the 'main thread'.

I want to use the terminology 'first_ever' to refer to the first
thread that constructed the global objects and entered the function
'main'.

Here's what C++ programmers are currently doing to get the ID of the
first thread:

std::thread::id g_id_1st{};

int main(void)
{
    g_id_1st = std::this_thread::get_id();
}

And then somewhere else:

if ( std::this_thread::get_id() == g_id_1st )
{
    std::cout << "main thread\n";
}

How about we make this easier and more convenient by providing:

    namespace std {
        class thread {
            static std::thread::id first_ever(void);
        };
    }

Received on 2023-04-30 10:37:58