C++ 11 thread not working on CodeBlocks

Started by
6 comments, last by ultramailman 11 years ago

I wanted to try out the new std::thread in C++11 in Code Blocks and I'm not sure if I'm doing it right.

This is the code:


#include <iostream>
#include <thread>

void foo()
{
    std::cout << "\n thread started. \n";
}

int main()
{
    std::cout << "starting thread";
    std::thread thr1(foo);

    std::cout << "waiting for thread to finish\n";
    thr1.join();

    std::cout << "done!\n";
    return 0;
}

At first I got some kind of warning that I had to use a compiler flag (-std=c++11), so I did.

And now I get 'thread is not a member of std'

I'm using GNU GCC compiler on windows 7. What should I do?

Advertisement
Works for me. What version of GCC do you have?

I'm guessing that you're using MinGW, which as far as I know doesn't support C++11's threading yet.

Yes I'm using MinGW. I didn't know it doesn't suport c++11 threading yet. I'll try something else. Thanks

If you use the GCC that comes bundled with Code::Blocks (TDM) then it does not support threads. Tthere are MinGW builds that do support thread. Maybe try a nightly build or something more up-to-date than what C::B ships packaged.

Ok I'll try the latest one I can find on the MinGW website.

I've encountered the same problem, and decided to implement the basic features of std::thread/std::mutex. You can use it yourself, the link is below. This small library has also features, i.e. read-write mutexes and semaphores implemented on Windows and POSIX. It isn't something complicated nor difficult to implement yourself, but saves some time.

http://ubuntuone.com/6rLDt59Gl8GWc6wgZsG3Nh

I think clang implements all of c++11 now, but I am not sure if it applies for Windows too. Still, it is good to check it out!

This topic is closed to new replies.

Advertisement