10

Is there anyway to use std::thread with C++11 standard library in windows when compiling with g++ 4.5.2 in MinGW?

I'm going assume no as I've seen many things saying you have to compile with the pthreads option but I figure I'd ask anyway.

NathanOliver
  • 150,499
  • 26
  • 240
  • 331
csteifel
  • 2,660
  • 4
  • 27
  • 55
  • It should work, but you'd need a newer gcc than 4.5.2 to get most of the C++0x features. Don't know what the minimum version is for `std::thread` support under mingw. – Ben Voigt Jul 21 '11 at 22:47
  • if thread library isn't included your next best option is boost::thread – Gene Bushuyev Jul 21 '11 at 22:54
  • Well I was gunna use WxWidgets threading since I'm already going to be using wxWidgets for the GUI part but I wanted to check to see if I could just use the new STL in its place – csteifel Jul 21 '11 at 23:21
  • @Anonymous: STL actually isn't the right term for this, STL is a very old library, whose design was mostly copied into the C++ Standard Library. – Ben Voigt Jul 22 '11 at 03:18
  • it is not C++0x anymore. It's c++11 or C++0B. – xis Aug 16 '11 at 18:06
  • Have a look at [the answer](http://stackoverflow.com/a/5931181/723845) – Loom Jan 18 '13 at 21:44
  • Possible duplicate of [mingw-w64 threads: posix vs win32](https://stackoverflow.com/questions/17242516/mingw-w64-threads-posix-vs-win32) – rogerdpack Oct 25 '18 at 04:37

2 Answers2

12

There is experimental support for std::thread in for MinGW-w64 toolchains.

Specifically, my GCC 4.6 builds provide usable std::thread through MinGW-w64's winpthreads library.

You can find downloads here:

Apart from that, MSVC11 (Visual Studio 2012) has <thread>, <chrono>, and <atomic>. You can download the Express edition here.

rubenvb
  • 69,525
  • 30
  • 173
  • 306
  • rubenvb, is this "my GCC 4.6 builds provide usable std::thread through MinGW-w64's winpthreads .." still valid? I downloaded the i686-w64-mingw32-gcc-4.6.3-release-win32_rubenvb.7z and tried to use std::mutex and std:: thread but I got "thread/mutex is not member of std. – Ragnar Jul 22 '12 at 12:48
  • 1
    @Ragnar: You'll need the newest builds in the "experimental" subdirectory, not release. I reorganized my downloads. Let me update the links in the answer. – rubenvb Jul 22 '12 at 12:51
  • Scrap that: it's "old", not "experimental" – rubenvb Jul 22 '12 at 12:58
  • @rubenvb, I know it's much later, but is this still going on at all? I would absolutely love being able to use this without giving up the additional C++11 features and updates added in 4.7.1 and 4.7.2. – chris Jan 07 '13 at 09:13
  • @chris see edit, and also: see my message in Lounge wrt Clang on Windows. – rubenvb Jan 07 '13 at 14:37
  • @rubenvb is there anyway to help get this into the stable releases and not require `-static`? – Matt Clarkson Feb 13 '13 at 16:26
  • @MattClarkson as a matter of fact, the `-static` part was fixed somewhere September last year. My "experimental" is just me being careful. The only thing that happens is that code depending on the libgcc DLL will depend on the winpthreads DLL. And there might be slight performance penalties, but I have no real reports so far. Note that the Qt project uses a std::thread-enabled toolchain for their official SDK releases. – rubenvb Feb 13 '13 at 20:48
  • @rubenvb OK, thanks for the info. I've only just got into using `g++` on Windows (so pretty new to all this) and have used your stable distribution, I guess I could use the experimental on from your downloads section. Which MinGW toolchain do they use? – Matt Clarkson Feb 14 '13 at 10:29
3

There is already a lightweight native implementation of std::thread and sync primitives: https://github.com/meganz/mingw-std-threads

IT is a header-only library and should work with any C++11 compliant version of MinGW. You just need to include the headers in your code.

Alexander Vassilev
  • 1,310
  • 13
  • 20