Bug 52680 - std::this_thread::sleep_for #ifdef'd out by _GLIBCXX_USE_NANOSLEEP
Summary: std::this_thread::sleep_for #ifdef'd out by _GLIBCXX_USE_NANOSLEEP
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.7.0
: P3 enhancement
Target Milestone: 4.8.0
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2012-03-23 01:35 UTC by Matt Kline
Modified: 2012-11-23 22:31 UTC (History)
2 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2012-03-31 00:00:00

A short test program (13 lines) illustrating the problem (146 bytes, text/x-c++src)
2012-03-23 01:35 UTC, Matt Kline

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Kline 2012-03-23 01:35:06 UTC
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
Comment 1 Jonathan Wakely 2012-03-23 12:45:48 UTC
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.
Comment 2 Matt Kline 2012-03-23 13:19:44 UTC
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?
Comment 3 Paolo Carlini 2012-03-23 13:34:31 UTC
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
Comment 4 Matt Kline 2012-03-30 19:19:03 UTC
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++
Comment 5 Jonathan Wakely 2012-03-31 12:02:43 UTC
(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?  ;)
Comment 6 Matt Kline 2012-03-31 14:52:40 UTC
Well this certainly isn't my brightest moment. That seems to be the problem.
Comment 7 Jonathan Wakely 2012-03-31 15:19:38 UTC
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."
Comment 8 Paolo Carlini 2012-04-17 09:43:26 UTC
This is a library issue isn't it?
Comment 9 Matt Kline 2012-04-17 16:35:51 UTC
Yes it is. Sorry for misclassifying it.
Comment 10 Jonathan Wakely 2012-06-05 18:15:28 UTC
some ideas at http://gcc.gnu.org/ml/libstdc++/2012-05/msg00085.html
Comment 11 Jonathan Wakely 2012-11-23 22:11:30 UTC
Author: redi
Date: Fri Nov 23 22:11:23 2012
New Revision: 193769

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=193769
	PR libstdc++/52680
	* 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):
	Declare unconditionally.
	* 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.

Comment 12 Jonathan Wakely 2012-11-23 22:31:14 UTC
fixed for 4.8, this_thread::sleep_for is always defined