Created attachment 26963 [details]
A short test program (13 lines) illustrating the problem
Inside the thread header for C++11, _GLIBCXX_USE_NANOSLEEP is not defined on my system, causing std::this_thread::sleep_for to be undefined. However, defining it manually using -D_GLIBCXX_USE_NANOSLEEP causes the attached test program to compile successfully and behave as intended.
I am using Ubuntu 10.04 LTS. The attached test code was successfully compiled using
g++ -std=c++11 test.cpp -pthread -D_GLIBCXX_USE_NANOSLEEP
Don't define that manually, you should configure gcc with --enable-libstdccxx-time instead.
We need to revisit the configure tests for nanosleep and sched_yield, as they are always available on some platforms, so we might be able to make --enable-libstdcxx-time the default, or make the configure checks more finegrained.
Thanks for shedding some light on this. Is there a reason that
--enable-libstdccxx-time isn't shown on http://gcc.gnu.org/install/configure.html?
Should be in the *library* configure docs, no? Anyway, looking forward, now that glibc is maintained in a slightly different way we should probably reconsider whether we could do better about this, in terms of defaults, etc.
By the way, on Linux, I normally build with --enable-libstdcxx-time=rt
Where should I specify that flag? _GLIBCXX_USE_NANOSLEEP is still undefined when I build gcc with
configure CFLAGS='-O3' --disable-bootstrap --disable-multilib --enable-ibstdccxx-time=rt --enable-languages=c,c++
(In reply to comment #4)
> Where should I specify that flag? _GLIBCXX_USE_NANOSLEEP is still undefined
> when I build gcc with
> configure CFLAGS='-O3' --disable-bootstrap --disable-multilib
> --enable-ibstdccxx-time=rt --enable-languages=c,c++
But what if you spell it correctly? ;)
Well this certainly isn't my brightest moment. That seems to be the problem.
ok, great :) As I said, we do need to revisit the configure-time checks which determine whether to use nanonsleep and sched_yield. Doing so is on my TODO list so I'm going to confirm this PR but as an enhancement rather than a bug.
For the benefit of the other maintainers: in some off-list discussion with Rainer I suggested that we might want to use --enable-libstdcxx-time by default on Solaris because sched_yield is defined in libposix4 and doesn't depend on libpthread. Additionally, we should note that librt(3LIB) on Solaris says: "The name libposix4 is maintained for backward compatibility and should be avoided. librt is the preferred name for this library."
This is a library issue isn't it?
Yes it is. Sorry for misclassifying it.
some ideas at http://gcc.gnu.org/ml/libstdc++/2012-05/msg00085.html
Date: Fri Nov 23 22:11:23 2012
New Revision: 193769
* acinclude.m4 (GLIBCXX_ENABLE_LIBSTDCXX_TIME): Check for usleep and
sleep if nanosleep is not available. Bump libtool revision.
* config.h.in: Regenerate.
* configure: Likewise.
* config/abi/pre/gnu.ver (GLIBCXX_3.4.18): Add __sleep_for.
* include/std/thread (this_thread::__sleep_for): Add.
(this_thread::yield, this_thread::sleep_until, this_thread::sleep_for):
* src/c++11/thread.cc (this_thread::__sleep_for): Define.
* testsuite/lib/libstdc++.exp (check_v3_target_nanosleep): Rename to
* testsuite/lib/dg-options.exp (dg-require-nanosleep): Rename to
* testsuite/30_threads/condition_variable_any/53830.cc: Update.
* testsuite/30_threads/this_thread/2.cc: Likewise.
* testsuite/30_threads/this_thread/3.cc: Likewise.
* testsuite/30_threads/this_thread/4.cc: Likewise.
* testsuite/30_threads/async/54297.cc: Likewise.
fixed for 4.8, this_thread::sleep_for is always defined