C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make std::thread not joinable after thread function ends.

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Sun, 18 Jun 2023 23:33:20 +0300
On Sun, 18 Jun 2023 at 23:26, Smith, Jim via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hi All,
>
> It would be convenient if std::thread would automatically become not joinable after the thread function ends.

Why? How is that convenient?

> In the below code snippet, thread1 is still joinable after its function ends.
>
> std::thread thread1([] () {
> std::cout << "Thread1 ended\n";
> });
>
> std::thread thread2([] () {
> for(long l = 0; l < 99999; ++l) { /* nothing */ }
> std::cout << "Thread2 ended\n";
> });
>
> thread2.join();
>
> if(thread1.joinable()) {
> std::cout << "Thread1 is joinable\n";
> }
> else {
> std::cout << "Thread1 is not joinable\n";
> }
>
> thread1.join();

I fail to see where the problem is. You have to either join or detach
a std::thread if it ever runs a thread function.

Received on 2023-06-18 20:33:34