This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: implementation of std::thread::hardware_concurrency()
- From: niXman <i dot nixman at gmail dot com>
- To: libstdc++ at gcc dot gnu dot org, gcc at gcc dot gnu dot org
- Date: Tue, 1 Nov 2011 19:35:58 +0400
- Subject: Re: implementation of std::thread::hardware_concurrency()
- References: <CAMPTgK2Fph+jQBtKQ+nHa7+BMdUCu=zT3dWaUO0pc7z=Xg8mWg@mail.gmail.com> <0223B132-51E6-4B54-8C2D-0EC690A5E9E0@oracle.com> <CAMPTgK0dyW0uP03vmOJ4N53vk0NwbFOgirGFqwz7wD2SFZhE_A@mail.gmail.com> <alpine.DEB.2.02.1111011251120.7409@laptop-mg.saclay.inria.fr>
> Er, the macro _GLIBCXX_NPROCS already handles
> the case sysconf(_SC_NPROCESSORS_ONLN).
> It looks like you actually want to remove the macro
> _GLIBCXX_NPROCS completely.
Fixed.
diff --git a/libstdc++-v3/src/thread.cc b/libstdc++-v3/src/thread.cc
index 09e7fc5..6feda4d 100644
--- a/libstdc++-v3/src/thread.cc
+++ b/libstdc++-v3/src/thread.cc
@@ -112,10 +112,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
unsigned int
thread::hardware_concurrency() noexcept
{
- int __n = _GLIBCXX_NPROCS;
- if (__n < 0)
- __n = 0;
- return __n;
+ int count = 0;
+#if defined(PTW32_VERSION) || \
+ (defined(__MINGW64_VERSION_MAJOR) && defined(_POSIX_THREADS)) || \
+ defined(__hpux)
+ count = pthread_num_processors_np();
+#elif defined(__APPLE__) || defined(__FreeBSD__)
+ size_t size = sizeof(count);
+ sysctlbyname("hw.ncpu", &count, &size, NULL, 0);
+#elif defined(_GLIBCXX_USE_GET_NPROCS) || \
+ defined(_GLIBCXX_USE_SC_NPROCESSORS_ONLN)
+ count = _GLIBCXX_NPROCS;
+#endif
+ return (count > 0) ? count : 0;
}
_GLIBCXX_END_NAMESPACE_VERSION
> Do you have already a Copyright assignment in place?
No. For public domain.