MinGW W64 NOT RECOGNIZING std::thread!

Hello. I recently updated my compiler to MinGW W64 version 5.1.0. I added

-std=c++1y

to the compiler configurations (I am using Code::Blocks). It seems that many of the C++14 features like std::make_shared() and std::make_unique() do work, however, there is no threading support!!

I #included the <thread> file properly, but the compiler gives me an error saying that thread was not declared in this scope. I decided to do some investigating on why this is happening and I opened the <thread> header file. The file exists and all the code including the code for the "thread" class was there, but for some strange reason there was no syntax highlighting at all, so it looked basically like a text document with C++ code in it, and the same thing happened to the <atomic> header file as well. I tried the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
#include <memory>
#include <thread>
#include <atomic>


int main()
{
  std::thread t1;
  auto ptr = std::make_shared<int>();
  *ptr = 3;
  
  std::cout << *ptr << std::endl;
  return 0;
}



I need some help with this issue as I plan to use heavy concurrency in my programs.


Also, I am using Windows Vista Home Premium.
Last edited on
however, there is no threading support!!


What are the exact error messages?

Did you add the thread library to your compiler settings?
The answers to your questions were already stated in my post:


I #included the <thread> file properly, but the compiler gives me an error saying that thread was not declared in this scope.


and


I decided to do some investigating on why this is happening and I opened the <thread> header file. The file exists and all the code including the code for the "thread" class was there, but for some strange reason there was no syntax highlighting at all, so it looked basically like a text document with C++ code in it, and the same thing happened to the <atomic> header file as well.



EDIT: How do I add the thread library to the Compiler Settings? I only added the #include directive to the file
Last edited on
What IDE are you using? And what is the actual version of the compiler you're using?

Since I don't do Windows I'm not really sure what switch you need to add, but for my Linux machine I had to add the "-pthread" switch to my compiler settings to tell the compile to use the proper thread library.

This link seems to be a good starting point. http://thispointer.com/c-11-multithreading-part-1-three-different-ways-to-create-threads/
Last edited on
All of these are also stated in my original post:


Hello. I recently updated my compiler to MinGW W64 version 5.1.0. I added

-std=c++1y

to the compiler configurations (I am using Code::Blocks).


And also: I added -lpthread to the Compiler Configuration as it said to do in the link you gave me. The compiler recognized this and it gave me no errors when I passed this as a command line argument.

HOWEVER:
std::thread still doesn't work!!! Does adding -lpthread allow you to only use POSIX threads? not the standard threading library? (by the way, using -pthread instead of -lpthread gives me an error saying that the file wasn't found.)

EDIT: Never mind, -pthread works. But I still can't use the standard threading library!!
Last edited on
Which MinGW exactly do you have? Who build it? What threading model does it use? What exception model does it use?

There are many different builds out there.
Last edited on
I don't know, I downloaded it at :

http://nuwen.net/mingw.html

It also includes the Boost library, but I want to just use the standard threading library.
Last edited on
What does gcc -v command shows? (in command line navigate to the directory where binaries are and call main gcc executable with key -v)

Also you can try this:
http://mingw-w64.org/doku.php/download/mingw-builds
or that: http://sourceforge.net/projects/mingw-w64/files/
Here is the link to the image of when I run g++ -v

http://s27.postimg.org/yq8cf57oj/SPECS.jpg

I don't know if it is the same for gcc -v

EDIT: IF the link doesn't work, it uses Win32 threading model.
Last edited on
That is the problem. Win32 threading model incompatible with C++11 threading. You should look for one with posix threads (or build upon winpthreads)
https://wiki.qt.io/MinGW-64-bit#GCC_Threading_model_.28posix_vs_win32.29
So if it uses Win32 threading model, will I be able to use threads from the Windows API?

EDIT:

I looked at that website, but I couldn't find a version that uses winpthreads and compiler version 5.1.0, only 4.8.0 and lower. Is there any other links with a more recent compiler version?
Last edited on
So if it uses Win32 threading model, will I be able to use threads from the Windows API?
You will be able to use them anyway. It just changes what runtime library uses internally.

I couldn't find a version that uses winpthreads and compiler version 5.1.0
Here it is:
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/5.1.0/threads-posix/seh/
Thank You.
For any serious concurrent programming using the standard C++11 thread library, strongly favour the Microsoft implementation on Windows. Either the Microsoft compiler or the (experimental) LLVM compiler (which too uses the Microsoft library).

There is absolutely nothing in Win32/Win64 threads that makes them incompatible with C++11 threads. The C++11 thread support library deliberately specifies very little; it was designed to be easily implementable on every commonly found platform; for instance the idea of support for thread pools was rejected because posix has no api support for thread pools. Microsoft had an implementation of the C++11 thread support library even when it was a draft standard.

On posix platforms, strongly favour clang++ / libc++. As far as possible, avoid the deadly cocktail of g++ / libstdc++ / pthreads which indiscriminately creates threads as if there is no tomorrow. Yes, it is a conforming implementation; C++ has nothing to say about how efficiently std::async() should be implemented.

The Windows threading model has excellent kernel support (on par with Solaris) for efficient thread pools; the Microsoft library implementation of std::async() exploits this (to the extent possible under strict conformance to C++11.)
Operating system threads are rather heavy-weight; it takes time and system resources to create a thread. If you have an algorithm that naturally decomposes into a large number of independent computations, a.k.a. tasks, you’ll probably get your best performance not by creating a separate thread for each task, but by adjusting the number of threads depending on the amount of parallelism available on your particular system, e.g., the number of cores and their current loads.
http://bartoszmilewski.com/2011/10/10/async-tasks-in-c11-not-quite-there-yet/


The LLVM implementation (libc++) on appears to have implemented a thread-pool layer of its own over the minimalist posix thread model.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <thread>
#include <string>
#include <chrono>
#include <set>
#include <mutex>
#include <vector>
#include <fstream>
#include <future>

namespace
{
    std::set<std::thread::id> ids ;
    std::mutex big_lock ;
}

std::string reverse( const std::string& str )
{
    {
        std::lock_guard<std::mutex> lock(big_lock) ;
        ids.insert( std::this_thread::get_id() ) ;
    }

    return { str.rbegin(), str.rend() } ;
}

int main()
{
    ids.insert( std::this_thread::get_id() ) ;
    
    
    std::ifstream file( __FILE__ ) ;
    std::vector< std::future<std::string> > futures ;

    std::string str ;
    // good implementations would (re)use a thread from the thread pool
    while( file >> str ) futures.push_back( std::async( std::launch::async, reverse, str ) );
    
    std::thread( reverse, "abracadabra" ).detach() ; // this must be a new thread

    for( auto& future : futures ) future.get() ;
    std::cout << "hardware concurrency: " << std::thread::hardware_concurrency() << '\n'
              << "#unique thread ids: " << ids.size() << '\n' ;
}


coliru:
clang++ --version | grep clang && clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp &&./a.out
echo ------------ && g++ --version | grep GCC && g++ -std=c++14 -g -O0 -Wall -Wextra -pedantic-errors -pthread main.cpp &&./a.out
clang version 3.6.0 (tags/RELEASE_360/final 235480)
hardware concurrency: 1
#unique thread ids: 6
------------
g++ (GCC) 5.1.0
hardware concurrency: 1
#unique thread ids: 135

rextester:
msc++: 1800
hardware concurrency: 1
#unique thread ids: 3


On this laptop, with (four cores), with msc++ and clang++ I get:
hardware concurrency: 4
#unique thread ids: varying from 8 to 16 

(Dynamic, because the constructor of std::lock_guard<> can block.)

With g++ / libstd++ / pthreads:
hardware concurrency: 4
#unique thread ids: 135

(no surprises here; create one new thread for each string in the file).
Still doesn't work. I created a new folder in C:\ called MinGW64 and copy and pasted all the files in there. I typed in gcc -v in command prompt and the threading model was posix, but it still says thread was not defined in std. Even when I open the <thread> file there is no highlighting
This one works for me (C++11 threads): http://tdm-gcc.tdragon.net/download
(I tried the 64-bit version)

In the Code Blocks IDE, Settings=>Compiler=>Toolchain executables=>Compiler's installation directory, specify the top level directory into which TDM-GCC was installed.

This IDE is somewhat brain-dead, so it won't be able to find the compiler on its own.
Settings=>Compiler=>Toolchain executables=>, set
C compiler: gcc.exe
C++ compiler: g++.exe
Linker for dynamic libs: g++.exe
Make program: make.exe

You may need to restart CodeBlocks.
Is there support for C++14?
> Is there support for C++14?

g++ (compiler): 'experimental' C++14 support is complete in GCC 5.1

libstdc++ (standard library): partial.
Almost all the missing features / non-conformance in the GNU library are C++11 features.
svn trunk:
C++11 status: https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2011
C++14 status: https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2014

Still and all, the library which ships with GCC 5.1 is quite good; a huge improvement over the woeful one that shipped with GCC 4.9.
this might help to use std::thread on minGW gcc compilers

https://github.com/meganz/mingw-std-threads





As already given you by JLBorges, you'll need TDM-GCC-w64. It is specifically designed to handle threads correctly (more or less), and uses winpthreads.

The MinGW-w64 compiler you must choose which threading model you will use, either "posix" (pthread) or "win32" (windows). In either case it does not do a very good job of handling C++ <thread>. (IIRC.)
Topic archived. No new replies allowed.