On Wed, 21 Apr 2021 at 20:41, Martin Liška <mli...@suse.cz> wrote:
>
> On 4/21/21 9:15 PM, Jakub Jelinek wrote:
> > On Wed, Apr 21, 2021 at 08:28:55PM +0200, Jakub Jelinek via Gcc-patches 
> > wrote:
> >>> There's a patch attempt for the problem with 
> >>> std::thread::hardware_concurrency where
> >>> it's used only if _GLIBCXX_HAS_GTHREADS is set.
> >>>
> >>> Does it help?
> >>> Thanks,
> >>> Martin
> >>>
> >>> gcc/ChangeLog:
> >>>
> >>>     PR bootstrap/100186
> >>>     * lto-wrapper.c: Use hardware_concurrency only if
> >>>     _GLIBCXX_HAS_GTHREADS.
> >>> ---
> >>>  gcc/lto-wrapper.c | 6 +++++-
> >>>  1 file changed, 5 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
> >>> index 6ba401007f6..8a85b3e93a8 100644
> >>> --- a/gcc/lto-wrapper.c
> >>> +++ b/gcc/lto-wrapper.c
> >>> @@ -1285,7 +1285,11 @@ run_gcc (unsigned argc, char *argv[])
> >>>    static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
> >>>
> >>>    /* Number of CPUs that can be used for parallel LTRANS phase.  */
> >>> -  unsigned long nthreads_var = std::thread::hardware_concurrency ();
> >>> +  unsigned long nthreads_var = 0;
> >>> +
> >>> +#ifdef _GLIBCXX_HAS_GTHREADS
> >>> +  nthreads_var = std::thread::hardware_concurrency ();
> >>> +#endif
> >>
> >> _GLIBCXX_HAS_GTHREADS is a libstdc++ internal macro, it shouldn't be used
> >> outside of libstdc++.
> >> And, when using some other compiler or standard C++ library, nthreads_var
> >> will be always 0.  That isn't an improvement.
> >
> > What would be IMHO a good idea would be to use configure test for
> > #include <thread>
> > int t = std::thread::hardware_concurrency ();
> > and in that case use that as a fallback to the previous implementation,
> > that will be strictly an improvement.
>
> That does not make sense to me. Original motivation was removal of the 
> complicated
> implementation with a C++ standard function.
>
> @Jonathan:
>
> What standard says about usage of std::thread::hardware_concurrency() [1]?
> The function is defined in C++11, how can one detect if threading is enabled
> in the C++ library and use it?

A configure test.

The standard says it's required to exist, but libstdc++ has
traditionally not defined things that are not supportable on the
target. So for targets with no multithreading, we didn't define
anything in <thread>. Even though that has changed in current releases
(so that std::thread exists even if it's useless), that won't help you
with previous releases. You need a configure test.

As I said in the PR, opinions differ on whether it's better to define
everything (even if it's useless) or not define them. Arguably, users
on small embedded targets do not want the library to waste space
defining a useless std::thread class that just throws an exception if
you ever try to create a new thread (but that's what the standard
requires).

Reply via email to