Discussion:
[Mingw-w64-public] libstdc++ threading for posix thread model in GCC on mingw
Ruben Van Boxem
2011-07-26 19:04:47 UTC
Permalink
Isn't that email subject a mouthful :)

In light of the previous discussion and my attempts to build a posix
threaded GCC, I have been looking through commit lists to find where the
checks are being done in the configury that do not enable libstdc++ threads
at the time of its compilation.

Let's gather up the facts:

C++0x std::thread is not automagically enabled. Specifically, I
needed to perform the following manual steps (taken from K. Frank's
http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTihttp://repo.or.cz/w/official-gcc.git/commit/9b0cec020065c5d0df370d14e6b5c0b8e18b6c79kwrRkzOuUdKOO2ShYSG7zqt8qq3QmXPfZsYhGa%40mail.gmail.com&forum_name=mingw-w64-public<http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTikwrRkzOuUdKOO2ShYSG7zqt8qq3QmXPfZsYhGa%40mail.gmail.com&forum_name=mingw-w64-public>
- Add "-D_POSIX_TIMEOUTS -D_GLIBCXX__PTHREADS
-D_GLIBCXX_HAS_GTHREADS" to the commandline, along with "-std=c++0x"
of course.
- Uncomment the line in bits/error_constants.h (strange, Explorer
won't show this file, but grep found it anyways... it's not hidden
either)
- And that ends up in an undefined reference to "std::thread::join()".
These all smell like bad configury at libstdc++ build time. I found these
commits (git version):

http://repo.or.cz/w/official-gcc.git/commit/968fd835d6eba899f937a1342b0a497b6188177e

http://repo.or.cz/w/official-gcc.git/commit/d5be2d8e8a35f4328f58d80debab5889dd68e32f

http://repo.or.cz/w/official-gcc.git/commit/a4290503e56ed1a53e90bccc4a6518b89b7c1e7a

http://repo.or.cz/w/official-gcc.git/commit/9b0cec020065c5d0df370d14e6b5c0b8e18b6c79

(they are listed newest to oldest)
These are all commits relating to setting some libstdc++ defines to enable
threading. I'm hoping there's a simple !mingw-like check that's easy to fix
in case of posix threaded gcc on all platforms, but the define relies on
several features (all related to atomic and threading functions/features
like nanosleep and condition variables) I do not know are available to us
when using winpthreads.

The checks need to be enabled for mingw targets, so the undefined reference
goes away when linking a program using std::thread. I hope the error
constant will magically also fix itself when this is fixed.

I do not know enough of autotools to figure this out though, any help is
appreciated.

I'll see if I can get a test build of GCC 4.6 (with Kai's posix threading
patches) built and uploaded soon.

Thanks!

Ruben
niXman
2011-07-26 19:26:15 UTC
Permalink
Hi Ruben!

I also try to build libstdc++ with winpthreads and determined what neither
in mingw-runtime and winpthreads are not implemented the required functions
nanosleep() and clock_gettime().
I finally built it :)
You need to add flags to configure script: "--enable-threads=posix
--enable-libstdcxx-time=rt" and create library with empty implementation of
these functions and build a library from them named librt.a

niXman
Post by Ruben Van Boxem
Isn't that email subject a mouthful :)
In light of the previous discussion and my attempts to build a posix
threaded GCC, I have been looking through commit lists to find where the
checks are being done in the configury that do not enable libstdc++ threads
at the time of its compilation.
C++0x std::thread is not automagically enabled. Specifically, I
needed to perform the following manual steps (taken from K. Frank's
http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTihttp://repo.or.cz/w/official-gcc.git/commit/9b0cec020065c5d0df370d14e6b5c0b8e18b6c79kwrRkzOuUdKOO2ShYSG7zqt8qq3QmXPfZsYhGa%40mail.gmail.com&forum_name=mingw-w64-public<http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTikwrRkzOuUdKOO2ShYSG7zqt8qq3QmXPfZsYhGa%40mail.gmail.com&forum_name=mingw-w64-public>
- Add "-D_POSIX_TIMEOUTS -D_GLIBCXX__PTHREADS
-D_GLIBCXX_HAS_GTHREADS" to the commandline, along with "-std=c++0x"
of course.
- Uncomment the line in bits/error_constants.h (strange, Explorer
won't show this file, but grep found it anyways... it's not hidden
either)
- And that ends up in an undefined reference to "std::thread::join()".
These all smell like bad configury at libstdc++ build time. I found these
http://repo.or.cz/w/official-gcc.git/commit/968fd835d6eba899f937a1342b0a497b6188177e
http://repo.or.cz/w/official-gcc.git/commit/d5be2d8e8a35f4328f58d80debab5889dd68e32f
http://repo.or.cz/w/official-gcc.git/commit/a4290503e56ed1a53e90bccc4a6518b89b7c1e7a
http://repo.or.cz/w/official-gcc.git/commit/9b0cec020065c5d0df370d14e6b5c0b8e18b6c79
(they are listed newest to oldest)
These are all commits relating to setting some libstdc++ defines to enable
threading. I'm hoping there's a simple !mingw-like check that's easy to fix
in case of posix threaded gcc on all platforms, but the define relies on
several features (all related to atomic and threading functions/features
like nanosleep and condition variables) I do not know are available to us
when using winpthreads.
The checks need to be enabled for mingw targets, so the undefined reference
goes away when linking a program using std::thread. I hope the error
constant will magically also fix itself when this is fixed.
I do not know enough of autotools to figure this out though, any help is
appreciated.
I'll see if I can get a test build of GCC 4.6 (with Kai's posix threading
patches) built and uploaded soon.
Thanks!
Ruben
------------------------------------------------------------------------------
Magic Quadrant for Content-Aware Data Loss Prevention
Research study explores the data loss prevention market. Includes in-depth
analysis on the changes within the DLP market, and the criteria used to
evaluate the strengths and weaknesses of these DLP solutions.
http://www.accelacomm.com/jaw/sfnl/114/51385063/
_______________________________________________
Mingw-w64-public mailing list
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
K. Frank
2011-07-26 20:18:41 UTC
Permalink
Hello niXman and Ruben!
Post by niXman
Hi Ruben!
I also try to build libstdc++ with winpthreads and determined what neither
in mingw-runtime and winpthreads are not implemented the required functions
nanosleep() and clock_gettime().
I finally built it :)
You need to add flags to configure script: "--enable-threads=posix
--enable-libstdcxx-time=rt" and create library with empty implementation of
these functions and build a library from them named librt.a
Some speculation on the nanosleep issue:

My copy of thread.cc (the gcc implementation for std::thread) has some code
protected by

#ifdef _GLIBCXX_USE_NANOSLEEP

When I compile thread.cc (without doing anything special in my environment)
_GLIBCXX_USE_NANOSLEEP appears not to be defined, and everything works.
However, if I define _GLIBCXX_USE_NANOSLEEP, e.g., on the g++ command
line, I get a compile-time error: "'::nanosleep' has not been declared".

So I speculate that nanosleep is an "optional" facility, perhaps provided by the
linux pthreads or other unix-style library.

Maybe if you can figure out where in your configuration _GLIBCXX_USE_NANOSLEEP
gets turned on, and figure out some way to turn it off, you won't need
it, and could
avoid providing the dummy implementation for it.

Just a thought...
Post by niXman
niXman
Good luck.
Post by niXman
Post by Ruben Van Boxem
Isn't that email subject a mouthful :)
...
These are all commits relating to setting some libstdc++ defines to enable
threading. I'm hoping there's a simple !mingw-like check that's easy to fix
in case of posix threaded gcc on all platforms, but the define relies on
several features (all related to atomic and threading functions/features
like nanosleep and condition variables) I do not know are available to us
when using winpthreads.
...
Ruben
Ruben, thanks for keeping after this. If there is anything you would like for
me to test out when you think you have std::thread working, I'd be happy to
give it a try.


Best.


K. Frank
Kai Tietz
2011-07-26 20:29:04 UTC
Permalink
Post by K. Frank
Hello niXman and Ruben!
Post by niXman
Hi Ruben!
I also try to build libstdc++ with winpthreads and determined what neither
in mingw-runtime and winpthreads are not implemented the required functions
nanosleep() and clock_gettime().
I finally built it :)
You need to add flags to configure script: "--enable-threads=posix
--enable-libstdcxx-time=rt" and create library with empty implementation of
these functions and build a library from them named librt.a
My copy of thread.cc (the gcc implementation for std::thread) has some code
protected by
   #ifdef _GLIBCXX_USE_NANOSLEEP
When I compile thread.cc (without doing anything special in my environment)
_GLIBCXX_USE_NANOSLEEP appears not to be defined, and everything works.
However, if I define _GLIBCXX_USE_NANOSLEEP, e.g., on the g++ command
line, I get a compile-time error: "'::nanosleep' has not been declared".
So I speculate that nanosleep is an "optional" facility, perhaps provided by the
linux pthreads or other unix-style library.
Maybe if you can figure out where in your configuration _GLIBCXX_USE_NANOSLEEP
gets turned on, and figure out some way to turn it off, you won't need
it, and could
avoid providing the dummy implementation for it.
Just a thought...
Post by niXman
niXman
Good luck.
Post by niXman
Post by Ruben Van Boxem
Isn't that email subject a mouthful :)
...
These are all commits relating to setting some libstdc++ defines to enable
threading. I'm hoping there's a simple !mingw-like check that's easy to fix
in case of posix threaded gcc on all platforms, but the define relies on
several features (all related to atomic and threading functions/features
like nanosleep and condition variables) I do not know are available to us
when using winpthreads.
...
Ruben
Ruben, thanks for keeping after this.  If there is anything you would like for
me to test out when you think you have std::thread working, I'd be happy to
give it a try.
Best.
K. Frank
Interesting. My last test with winpthread an libstdc++ didn't shown
this issues about nanosleep (and this clock_time thing).

Both functions aren't present on a Windows native environment. You
might can write emulation code for them, but they are not part of
standard runtime.

So I assume you have enabled here some linux specific parts, which
aren't supported.

Kai
niXman
2011-07-26 20:46:34 UTC
Permalink
Post by Kai Tietz
Both functions aren't present on a Windows native environment. You
might can write emulation code for them, but they are not part of
standard runtime.
So I assume you have enabled here some linux specific parts, which
aren't supported.
I know that they are not part of windows runtime. But rereading the
source libstdc
+ +, I realized that nanosleep() is used for std::this_thread::sleep_for(),
and clock_gettime() is used to obtain the system time in
std::this_thread::sleep_until(). Maybe something I noticed ...
niXman
2011-07-31 03:25:24 UTC
Permalink
Hi all!
Post by K. Frank
My copy of thread.cc (the gcc implementation for std::thread) has some code
protected by
#ifdef _GLIBCXX_USE_NANOSLEEP
When I compile thread.cc (without doing anything special in my environment)
_GLIBCXX_USE_NANOSLEEP appears not to be defined, and everything works.
However, if I define _GLIBCXX_USE_NANOSLEEP, e.g., on the g++ command
line, I get a compile-time error: "'::nanosleep' has not been declared".
So I speculate that nanosleep is an "optional" facility, perhaps provided by the
linux pthreads or other unix-style library.
No!
Post by K. Frank
259#ifdef _GLIBCXX_USE_NANOSLEEP 260 /// sleep_for 261 template<typename
_Rep, typename _Period> 262 inline void 263 sleep_for(const
chrono::duration<_Rep, _Period>& __rtime) 264 { 265 chrono::seconds __s =
266 chrono::duration_cast<chrono::seconds>(__rtime); 267 268chrono::nanoseconds __ns =
269 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s); 270 271__gthread_time_t __ts =
272 { 273 static_cast<std::time_t>(__s.count()), 274static_cast<long>(__ns.count())
275 }; 276 277 ::nanosleep(&__ts, 0); 278 } 279 280 /// sleep_until 281template<typename _Clock, typename _Duration>
282 inline void 283 sleep_until(const chrono::time_point<_Clock,
_Duration>& __atime) 284 { sleep_for(__atime - _Clock::now()); } 285#endif
If _GLIBCXX_USE_NANOSLEEP is not defined then std::thread::sleep_for() and
std::thread::sleep_until() will be excluded.

niXman.
niXman
2011-07-31 03:43:39 UTC
Permalink
---------- Forwarded message ----------
From: niXman <***@gmail.com>
Date: 2011/7/31
Subject: Re: [Mingw-w64-public] libstdc++ threading for posix thread model
in GCC on mingw
To: Mingw-w64-***@lists.sourceforge.net

Oops :)
Look the code from thread header file:
259#ifdef _GLIBCXX_USE_NANOSLEEP 260 /// sleep_for 261 template<typename
_Rep, typename _Period> 262 inline void 263 sleep_for(const
chrono::duration<_Rep, _Period>& __rtime) 264 { 265 chrono::seconds __s =
266 chrono::duration_cast<chrono::seconds>(__rtime); 267
268chrono::nanoseconds __ns =
269 chrono::duration_cast<chrono::nanoseconds>(__rtime - __s); 270
271__gthread_time_t __ts =
272 { 273 static_cast<std::time_t>(__s.count()),
274static_cast<long>(__ns.count())
275 }; 276 277 ::nanosleep(&__ts, 0); 278 } 279 280 /// sleep_until
281template<typename _Clock, typename _Duration>
282 inline void 283 sleep_until(const chrono::time_point<_Clock, _Duration>&
__atime) 284 { sleep_for(__atime - _Clock::now()); } 285#endif

niXman.

Loading...