Discussion:
[Mingw-w64-public] std::this_thread::sleep_for not working
K. Frank
2012-05-13 23:40:55 UTC
Permalink
Hello All!

I am finding that std::this_thread::sleep_for is not fully/properly implemented.

I am using Ruben's 64-bit mingw-w64 4.7.0 <thread>-enabled build.

When I try compiling:

g++ -std=c++0x -static -o test_sleep test_sleep.cpp

the line:

std::this_thread::sleep_for (std::chrono::milliseconds(500));

I get the error:

error: 'sleep_for' is not a member of 'std::this_thread'

When I try compiling with the following magic incantation:

g++ -D_GLIBCXX_USE_NANOSLEEP -std=c++0x -static -o test_sleep test_sleep.cpp

I get the error:

In file included from test_sleep.cpp:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../include/c++/4.7.0/thread:
In function 'void std::this_thread::sleep_for(const
std::chrono::duration<_Rep, _Period>&)':
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.0/../../../../include/c++/4.7.0/thread:277:2:
error: '::nanosleep' has not been declared

Some background:

Going back over my notes I see that when I was building my <thread>
implementation I came across this same issue, but I never came back
to sort it out or implement it properly. (At the time I was focused on
using sleep_for as part of a test for some of the other <thread> stuff
I had implemented, rather than on implementing sleep_for itself. So
I just moved forward by using the native windows sleep for my testing
and overlooked coming back to get sleep_for working.)

This looks like a semi-know issue in the gcc / linux world. Here are
a couple of semi-random references:

http://markmail.org/message/ufyrfwq6qjphewbm#query:+page:1+mid:4mui756gcycjvjnx+state:results
https://bugs.archlinux.org/task/27751

(Hence my attempt to us -D_GLIBCXX_USE_NANOSLEEP.)


Best.


K. Frank
xunxun
2012-05-14 07:01:12 UTC
Permalink
Post by K. Frank
Hello All!
I am finding that std::this_thread::sleep_for is not fully/properly implemented.
I am using Ruben's 64-bit mingw-w64 4.7.0<thread>-enabled build.
g++ -std=c++0x -static -o test_sleep test_sleep.cpp
std::this_thread::sleep_for (std::chrono::milliseconds(500));
error: 'sleep_for' is not a member of 'std::this_thread'
g++ -D_GLIBCXX_USE_NANOSLEEP -std=c++0x -static -o test_sleep test_sleep.cpp
In function 'void std::this_thread::sleep_for(const
error: '::nanosleep' has not been declared
Going back over my notes I see that when I was building my<thread>
implementation I came across this same issue, but I never came back
to sort it out or implement it properly. (At the time I was focused on
using sleep_for as part of a test for some of the other<thread> stuff
I had implemented, rather than on implementing sleep_for itself. So
I just moved forward by using the native windows sleep for my testing
and overlooked coming back to get sleep_for working.)
This looks like a semi-know issue in the gcc / linux world. Here are
http://markmail.org/message/ufyrfwq6qjphewbm#query:+page:1+mid:4mui756gcycjvjnx+state:results
https://bugs.archlinux.org/task/27751
(Hence my attempt to us -D_GLIBCXX_USE_NANOSLEEP.)
Best.
K. Frank
I think it need to rebuild gcc using --enable-libstdcxx-time=yes
--
Best Regards,
xunxun
Ruben Van Boxem
2012-05-14 07:15:33 UTC
Permalink
Post by xunxun
Post by K. Frank
Hello All!
I am finding that std::this_thread::sleep_for is not fully/properly implemented.
I am using Ruben's 64-bit mingw-w64 4.7.0<thread>-enabled build.
g++ -std=c++0x -static -o test_sleep test_sleep.cpp
std::this_thread::sleep_for (std::chrono::milliseconds(500));
error: 'sleep_for' is not a member of 'std::this_thread'
g++ -D_GLIBCXX_USE_NANOSLEEP -std=c++0x -static -o test_sleep test_sleep.cpp
In function 'void std::this_thread::sleep_for(const
error: '::nanosleep' has not been declared
Going back over my notes I see that when I was building my<thread>
implementation I came across this same issue, but I never came back
to sort it out or implement it properly. (At the time I was focused on
using sleep_for as part of a test for some of the other<thread> stuff
I had implemented, rather than on implementing sleep_for itself. So
I just moved forward by using the native windows sleep for my testing
and overlooked coming back to get sleep_for working.)
This looks like a semi-know issue in the gcc / linux world. Here are
http://markmail.org/message/ufyrfwq6qjphewbm#query:+page:1+mid:4mui756gcycjvjnx+state:results
Post by xunxun
Post by K. Frank
https://bugs.archlinux.org/task/27751
(Hence my attempt to us -D_GLIBCXX_USE_NANOSLEEP.)
Best.
K. Frank
I think it need to rebuild gcc using --enable-libstdcxx-time=yes
I'll add that in my next build. The question becomes then why is this not
detected by configure automatically?

Ruben
Post by xunxun
--
Best Regards,
xunxun
------------------------------------------------------------------------------
Post by xunxun
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mingw-w64-public mailing list
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
xunxun
2012-05-14 07:24:53 UTC
Permalink
Post by Ruben Van Boxem
I'll add that in my next build. The question becomes then why is this
not detected by configure automatically?
Maybe Kai knows.

This happened in gcc4.6.x first, so I use --enable-libstdcxx-time=yes
from then.
--
Best Regards,
xunxun
Kai Tietz
2012-05-14 08:10:56 UTC
Permalink
Post by xunxun
Post by Ruben Van Boxem
I'll add that in my next build. The question becomes then why is this
not detected by configure automatically?
Maybe Kai knows.
This happened in gcc4.6.x first, so I use --enable-libstdcxx-time=yes
from then.
--
Best Regards,
xunxun
Well, AFAIU is libstdc++ here checking for presence of nanosleep.
Actual we provide '_sleep' (and usleep), but no nanosleep routine as
it is POSIX. If you are using winpthread then this routine is provide
by it, but of course it is just present if pthread is linked.
So --enable-libstdcxx-time=yes seems to me as valid and the best way
to solve this issue.

Kai
K. Frank
2012-05-14 15:08:04 UTC
Permalink
Hi Guys!
Post by Kai Tietz
Post by xunxun
Post by Ruben Van Boxem
I'll add that in my next build. The question becomes then why is this
not detected by configure automatically?
Maybe Kai knows.
This happened in gcc4.6.x first, so I use --enable-libstdcxx-time=yes
from then.
Best Regards,
xunxun
Well, AFAIU is libstdc++ here checking for presence of nanosleep.
Actual we provide '_sleep' (and usleep), but no nanosleep routine as
it is POSIX.  If you are using winpthread then this routine is provide
by it, but of course it is just present if pthread is linked.
So --enable-libstdcxx-time=yes seems to me as valid and the best way
to solve this issue.
Kai
Thanks for the follow-up. This is in no way urgent for me. Native
windows Sleep is fine for me for the simple stuff I'm doing. But if
you release another <thread>-enabled build with sleep_for included,
I'll plan to give it a try.

By the way, a quick semi-related question: To use native windows
Sleep I am including windows.h. It's not a big deal, but it does
increase the compile time of simple test programs noticeably.
(Not really a lot, though.) Would anyone know off-hand if I can
just put in two or three lines of prototype declarations for Sleep
instead of including all of windows.h?

Thanks to all.


K. Frank
Joshua Boyce
2012-05-15 07:54:59 UTC
Permalink
Post by K. Frank
By the way, a quick semi-related question: To use native windows
Sleep I am including windows.h. It's not a big deal, but it does
increase the compile time of simple test programs noticeably.
(Not really a lot, though.) Would anyone know off-hand if I can
just put in two or three lines of prototype declarations for Sleep
instead of including all of windows.h?
Thanks to all.
Yes, as long as you add a __declspec(dllimport) to the function declaration
and also link in the appropriate import library.

Oh, you will also need an 'extern "C"' to prevent name mangling afaik.

Boost for example manually declares and imports all the Windows APIs it
uses unless you define BOOST_USE_WINDOWS_H. If you want to check out what
they're doing as a confirmation what you're doing is correct I believe
their wrappers are in boost/detail/win.
K. Frank
2012-05-15 12:34:13 UTC
Permalink
Hi Joshua!

On Tue, May 15, 2012 at 3:54 AM, Joshua Boyce
Post by Joshua Boyce
By the way, a quick semi-related question:  To use native windows
Sleep I am including windows.h.  It's not a big deal, but it does
increase the compile time of simple test programs noticeably.
(Not really a lot, though.)  Would anyone know off-hand if I can
just put in two or three lines of prototype declarations for Sleep
instead of including all of windows.h?
Thanks to all.
...
Yes, as long as you add a __declspec(dllimport) to the function declaration
and also link in the appropriate import library.
I don't follow about linking to an (additional) appropriate import library.
Presumably I am already linking correctly when I include windows.h
and use Sleep in my code. Correct me if I'm wrong, but including
windows.h shouldn't automatically be causing me to link any differently.
Post by Joshua Boyce
Oh, you will also need an 'extern "C"' to prevent name mangling afaik.
Yes, that sounds right.
Post by Joshua Boyce
Boost for example manually declares and imports all the Windows APIs it uses
unless you define BOOST_USE_WINDOWS_H. If you want to check out what they're
doing as a confirmation what you're doing is correct I believe their
wrappers are in boost/detail/win.
Thanks. I should be able to sort this out by rummaging through
mingw-w64's windows.h and the boost headers. But any hints in
advance what the sleep declaration might end up looking like in
advance?

I appreciate your help.


K. Frank
Ruben Van Boxem
2012-05-15 12:37:24 UTC
Permalink
Post by K. Frank
Hi Joshua!
On Tue, May 15, 2012 at 3:54 AM, Joshua Boyce
Post by Joshua Boyce
Post by K. Frank
By the way, a quick semi-related question: To use native windows
Sleep I am including windows.h. It's not a big deal, but it does
increase the compile time of simple test programs noticeably.
(Not really a lot, though.) Would anyone know off-hand if I can
just put in two or three lines of prototype declarations for Sleep
instead of including all of windows.h?
Thanks to all.
...
Yes, as long as you add a __declspec(dllimport) to the function
declaration
Post by Joshua Boyce
and also link in the appropriate import library.
I don't follow about linking to an (additional) appropriate import library.
Presumably I am already linking correctly when I include windows.h
and use Sleep in my code. Correct me if I'm wrong, but including
windows.h shouldn't automatically be causing me to link any differently.
True. GCC links all standard windows libs automagically.
Post by K. Frank
Post by Joshua Boyce
Oh, you will also need an 'extern "C"' to prevent name mangling afaik.
Yes, that sounds right.
Post by Joshua Boyce
Boost for example manually declares and imports all the Windows APIs it
uses
Post by Joshua Boyce
unless you define BOOST_USE_WINDOWS_H. If you want to check out what
they're
Post by Joshua Boyce
doing as a confirmation what you're doing is correct I believe their
wrappers are in boost/detail/win.
Thanks. I should be able to sort this out by rummaging through
mingw-w64's windows.h and the boost headers. But any hints in
advance what the sleep declaration might end up looking like in
advance?
http://msdn.microsoft.com/en-us/library/ms686298%28v=vs.100%29.aspx
Post by K. Frank
I appreciate your help.
K. Frank
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mingw-w64-public mailing list
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
K. Frank
2012-05-15 13:07:16 UTC
Permalink
Hello Ruben!

On Tue, May 15, 2012 at 8:37 AM, Ruben Van Boxem
Post by Ruben Van Boxem
Post by K. Frank
Hi Joshua!
On Tue, May 15, 2012 at 3:54 AM, Joshua Boyce
By the way, a quick semi-related question:  To use native windows
Sleep I am including windows.h.  It's not a big deal, but it does
increase the compile time of simple test programs noticeably.
(Not really a lot, though.)  Would anyone know off-hand if I can
just put in two or three lines of prototype declarations for Sleep
instead of including all of windows.h?
...
Thanks.  I should be able to sort this out by rummaging through
mingw-w64's windows.h and the boost headers.  But any hints in
advance what the sleep declaration might end up looking like?
http://msdn.microsoft.com/en-us/library/ms686298%28v=vs.100%29.aspx
Yeah, per your msdn link:

VOID WINAPI Sleep(
__in DWORD dwMilliseconds
);

and per mingw-w64's winbase.h:

WINBASEAPI VOID WINAPI Sleep(DWORD dwMilliseconds);

I sort of knew that already. But it's the "VOID" and "WINAPI" and
"DWORD", etc that I need to rummage through the headers to
translate properly.

Well ... After some rummaging I came up with:

extern "C" {
__declspec(dllimport) void __stdcall Sleep (unsigned long dwMilliseconds);
}

How does that look? It seems to work.

And it shaves about six seconds (about 40%) off of the compilation
time of a small, three-file test program. (Oh, well ... Less time to
read xkcd ... http://xkcd.com/303/)

Thanks.


K. Frank
Joshua Boyce
2012-05-16 10:46:02 UTC
Permalink
Post by K. Frank
And it shaves about six seconds (about 40%) off of the compilation
time of a small, three-file test program. (Oh, well ... Less time to
read xkcd ... http://xkcd.com/303/)
What is the time difference if you continue to use Windows.h, but define
WIN32_LEAN_AND_MEAN?

http://msdn.microsoft.com/en-us/library/aa383745(v=vs.85).aspx#faster_builds_with_smaller_header_files

e.g.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
Joshua Boyce
2012-05-16 10:44:17 UTC
Permalink
On Tue, May 15, 2012 at 10:37 PM, Ruben Van Boxem
Post by K. Frank
Hi Joshua!
Post by K. Frank
On Tue, May 15, 2012 at 3:54 AM, Joshua Boyce
Post by Joshua Boyce
Post by K. Frank
By the way, a quick semi-related question: To use native windows
Sleep I am including windows.h. It's not a big deal, but it does
increase the compile time of simple test programs noticeably.
(Not really a lot, though.) Would anyone know off-hand if I can
just put in two or three lines of prototype declarations for Sleep
instead of including all of windows.h?
Thanks to all.
...
Yes, as long as you add a __declspec(dllimport) to the function
declaration
Post by Joshua Boyce
and also link in the appropriate import library.
I don't follow about linking to an (additional) appropriate import library.
Presumably I am already linking correctly when I include windows.h
and use Sleep in my code. Correct me if I'm wrong, but including
windows.h shouldn't automatically be causing me to link any differently.
True. GCC links all standard windows libs automagically.
I didn't mean that an additional import library was required, simply that
the correct import lib needs to be linked in as normal (just like you do
when using windows.h). For functions exported by kernel32 this
happens automatically, for other modules (user32, advapi32, etc) I believe
they must be specified manually. Is this not true? (I'm genuinely
interested, as I thought it was true last time I checked, but it may have
just been a configuration error on my end.)
Earnie Boyd
2012-05-16 12:18:05 UTC
Permalink
On Wed, May 16, 2012 at 6:44 AM, Joshua Boyce
Post by Joshua Boyce
I didn't mean that an additional import library was required, simply that
the correct import lib needs to be linked in as normal (just like you do
when using windows.h). For functions exported by kernel32 this
happens automatically, for other modules (user32, advapi32, etc) I believe
they must be specified manually. Is this not true? (I'm genuinely
interested, as I thought it was true last time I checked, but it may have
just been a configuration error on my end.)
Assuming you use GCC/G++ for the linker command user32 and advapi32
are added as libraries. To see the list of libraries added use -v and
you will see the full linker command parameters as passed to it by
GCC.
--
Earnie
-- https://sites.google.com/site/earnieboyd
Joshua Boyce
2012-05-17 08:59:05 UTC
Permalink
On Wed, May 16, 2012 at 10:18 PM, Earnie Boyd
Post by Earnie Boyd
On Wed, May 16, 2012 at 6:44 AM, Joshua Boyce
Post by Joshua Boyce
I didn't mean that an additional import library was required, simply that
the correct import lib needs to be linked in as normal (just like you do
when using windows.h). For functions exported by kernel32 this
happens automatically, for other modules (user32, advapi32, etc) I
believe
Post by Joshua Boyce
they must be specified manually. Is this not true? (I'm genuinely
interested, as I thought it was true last time I checked, but it may have
just been a configuration error on my end.)
Assuming you use GCC/G++ for the linker command user32 and advapi32
are added as libraries. To see the list of libraries added use -v and
you will see the full linker command parameters as passed to it by
GCC.
--
Earnie
-- https://sites.google.com/site/earnieboyd
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mingw-w64-public mailing list
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
Interesting, I'll have to look at my build config then. Thanks.

Loading...