Hello All -

If you want to use std::thread with mingw before official support gets
fully integrated, it's pretty easy to get std::thread working by hooking
it up to pthreads-win32.  (There are licensing issues, however, with
pthreads-win32.)

(std::thread is part of the new c++0x standard, and gcc's support for
it is "experimental.")

I have used the following recipe with both mingw32 and mingw-w64,
and I have run some (not all) of the gcc's std::thread test suite,
and I've run some of my own test programs that exercise various
threading and synchronization functionality.

Note, std::future does not work for me with mingw32 using g++ version
"g++ (GCC) 4.5.0".  (It does work with mingw-w64 / 4.5.2.  I am guessing
that this is a 4.5.0 issue rather than a mingw32 issue, and seems to be
caused by not having support for _GLIBCXX_ATOMIC_BUILTINS_4 turned
on.)


Recipe:

1)  Download and install mingw.

   mingw32 -- I ran

      mingw-get update
      mingw-get install g++
      mingw-get install pthreads

   (I did this a while ago and ended up with g++ version "g++ (GCC) 4.5.0".)

   mingw-w64 -- I downloaded a "sezero" build:

      
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/sezero_4.5_20101002/mingw-w64-bin_x86_64-mingw_20101002_4.5_sezero.zip/download
      uncompressed it
      uncompressed the file "pthreads-w64.zip" that was in mingw-w64's root
         directory

   (Note, I need the 4.5 version.  The 4.4 version had a problem with
      std::bind.)

2)  Get copies of thread.cc, mutex.cc, and condition_variable.cc.

   I got them from:

      http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/src/

   i.e.:

      http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/src/thread.cc?view=log
      http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/src/mutex.cc?view=log
      
http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/src/condition_variable.cc?view=log

3)  Patch error_constants.h:

   Uncomment the line (line 100):

      //    operation_not_permitted = EPERM,

   in:

      .\mingw\lib\gcc\mingw32\4.5.0\include\c++\mingw32\bits\error_constants.h
 (mingw32)
      .\mingw64\include\c++\4.5.2\x86_64-w64-mingw32\bits\error_constants.h
    (mingw-w64)

3)  Patch thread:

   replace (starting at line 90 -- uncommented in the original):

      // friend bool
      // operator<(thread::id __x, thread::id __y)
      // { return __x._M_thread < __y._M_thread; }

   with:

      friend bool
      operator<(thread::id __x, thread::id __y)
      {
        return (
          (__x._M_thread.p < __y._M_thread.p)  ||
          ( (__x._M_thread.p == __y._M_thread.p) &&
          (__x._M_thread.x < __y._M_thread.x) )
        );
      }

   in:

      .\mingw\lib\gcc\mingw32\4.5.0\include\c++\thread  (mingw32)
      .\mingw64\include\c++\4.5.2\thread                (mingw-w64)

4)  Compile as follows:

   use the flags:

      -D_POSIX_TIMEOUTS
      -D_GLIBCXX__PTHREADS
      -D_GLIBCXX_HAS_GTHREADS
      -std=c++0x
      -enable-auto-import

   (This last -- -enable-auto-import -- suppresses warnings with my
   copy of mingw32, and isn't needed with my copy of mingw-w64.)

   add the source files:

      thread.cc
      mutex.cc
      condition_variable.cc

   add the library:

      -lpthread

   Thus, for example, to compile "std_thread_test.cpp":

      g++ -D_POSIX_TIMEOUTS -D_GLIBCXX__PTHREADS -D_GLIBCXX_HAS_GTHREADS
         -enable-auto-import -std=c++0x
         -o std_thread_test std_thread_test.cpp
         thread.cc mutex.cc condition_variable.cc -lpthread

   (Note, whether or not you need mutex.cc and condition_variable.cc
   depends on which std::thread features you use.)


I like std::thread so far.  It looks like a sensible threading and
synchronization api, using the facilities and idioms of modern c++.

Best.


K. Frank

------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to