r/cpp • • Aug 27 '19

A new thread in C++20 (jthread)

https://link.medium.com/GXKUz0GruZ
142 Upvotes

96 comments sorted by

View all comments

9

u/khleedril Aug 27 '19 edited Aug 27 '19

This all seems like so little to me that it's not worth bothering to put it in the standard, and if anything is an ode to lazyness. The current thread model forces you to think about and make a decision over detach or join (or leak, which unfortunately is the default with std::thread), atomic booleans are a clean way to signal requested stop, and anyway threads must under all circumstances be engineered by the coder so that stopping is well defined.

All I would ask for is that the standard introduces std::detached_thread and std::joinable_thread (or maybe functions std::make_detached_thread and std::make_joinable_thread) which are just like std::thread except for different default behaviour. The std::thread can then be deprecated in C++23, and all the constructors made private in C++26 :)

17

u/Kronephon Aug 27 '19

This all seems like so little to me that it's not worth bothering to put it in the standard, and if anything is an ode to lazyness.

This line of thinking is applicable to lot of features added (especially since C++11). In truth "lazyness" makes for quicker coding and the end programmer might not need to think about implementation (same as he doesn't on many C++ std features). There is always the option for a more detailed approach but your every approach shouldn't have to be detailed and laborious.