CppCoreGuidelines
CppCoreGuidelines copied to clipboard
CP.25: gsl::joining_thread missing implementation in Guidelines support library
Problem
CP.25 says "Prefer gsl::joining_thread over std::thread". However there is no implementation of gsl::joining_thread
in Microsoft GSL. Hence the rule CP.25 is incorrect.
Workaround
For projects using C++20, CP.25 could be reworded to "Prefer std::jthread to std::thread". For projects using C++11 & higher, there should be a workaround to handle this broken rule.
The textbook "C++ Concurrency in action - Practical Multithreading" by Anthony Williams, Chapter 2: "Managing threads", Section 2.3: "Transferring ownership of a thread" under Listing 2.6: "scoped_thread and example usage" mentions an example implementation of scoped_thread
class which could be a useful workaround to handle this scenario for projects lesser than C++20 version.
Rainer Grimm's blog also mentions about this workaround.
Action item
It would be nice to incorporate the above workarounds or a better solution to address the incorrect CP.25 rule. If this workarounds are okay for reviewers, I can support in creating a pull request to edit the rule. Let me know your valuable thoughts on this.
Thanks, Honey
Honey Sukesan
Guidelines describe what GSL should provide, implementations do their best to make it happen.
If there's interest in finalizing Microsoft's implementation of gsl::joining_thread, perhaps restarting https://github.com/microsoft/GSL/pull/582 would help, but it's unlikely, given C++20.
See also https://github.com/isocpp/CppCoreGuidelines/issues/1503 - if anything I imagine guidelines will just switch to std::jthread
as part of wider C++20 rebase.
Hi,
Thank you for sharing your thoughts.
However I think a vast majority of C++ community are still not using C++20 version in their projects.
For a sample survey results, please have a look at https://www.cppstories.com/2022/cpp-status-2022/#your-input--survey
I believe C++ core guidelines should be applicable for anyone using C++14 & higher. In that case, I would be more inclined to rewording the guideline considering two specific scenarios - C++ 20 users & for others. Maintaining gsl:: joining_thread in the rule is incorrect in my opinion as it's just a wishlist & nobody is going to benefit from it.
What do you think?
Well, the README says:
The aim of the guidelines is to help people to use modern C++ effectively. By "modern C++" we mean C++11 and newer.
That sentence was introduced in 2015 (back then it listed versions instead of just saying "and newer"), four years after C++11 was standardized. So I guess rewriting the guidelines for "C++20 and newer" will not happen in the near future anyway. But I'm not an editor.