0
votes

I've got an error that I don't understand using std::thread and SFML2.1.

I didn't have this error while developping with DevCPP + TDM-GCC, but I now use Code::Blocks + TDM-GCC (Windows 7).

Basically I don't have any compilation error / warning, but I get a program not responding on thread joins if I've got a SFML RenderWindow instantiated in the main.

I tried to ask our Google friend if he new anything about that but it didn't retrieve any result that helped, appart from recompiling SFML with Code::Blocks, "just to be sure"...

Here is a very simplified piece of code on which I still get the error (I already managed to get a window with texture and text in SFML, just to but aside SFML library config problems) :

#include <SFML/Graphics.hpp>
#include <thread>
#include <iostream>

int main(int, char const**)
{
    std::thread my_thread([](){
            std::cout<<"toto"<<'\n';
    });
    my_thread.join();
    //my_thread.detach();
    std::cout<<"tata"<<'\n';

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    return EXIT_SUCCESS;
}

If I replace my_thread.join() with my_thread.detach(), the error goes away.

If I delete the RenderWindow instatiation, the error goes away.

Any Idea ?

Edit 2: updated build log

mingw32-g++.exe -std=c++11 -Wall -pthread -lpthread -std=c++11 -g -pthread -IC:\SFML-2.1\include -Ijabos -IC:\SFML-2.1\include -c C:\Users\frup82554\Documents\svn\jabos\jabos\main.cpp -o jabos.cb\obj\Debug\jabos\main.o
mingw32-g++.exe -LC:\SFML-2.1\lib -LC:\SFML-2.1\lib -o jabos.cb\bin\Debug\jabos.exe jabos.cb\obj\Debug\jabos\Couple.o jabos.cb\obj\Debug\jabos\Force.o jabos.cb\obj\Debug\jabos\main.o jabos.cb\obj\Debug\jabos\Masse.o jabos.cb\obj\Debug\jabos\Masse1d.o jabos.cb\obj\Debug\jabos\ThreadPool.o jabos.cb\obj\Debug\jabos\ValeurNDimension.o  -lpthread -pthread -lpthread  -lmingw32 -luser32 -lgdi32 -lwinmm -ldxguid -lsfml-graphics-d -lsfml-window-d -lsfml-system-d
Output file is jabos.cb\bin\Debug\jabos.exe with size 1.62 MB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
1
Please post the command line that gcc is being invoked with. Make sure the -pthread switch is enabled. - Praetorian
I tried to add the -pthread switch and still got the error. Anyway isn't this switch supposed to be about "pthread.h" ? I'm using c++11 threads which are different, aren't they ? (I added the build logs as requested) - Tyrendel
I think this problem is mentioned somewhere in SFML's thread tutorial -- sfml-dev.org/tutorials/2.0/system-thread.php - 0x499602D2
I read this tutorial and I didn't see anything about my problem (or any problem regarding std::thread). It is written: "If you work with compilers that support the new standard and its <thread> header, forget about SFML classes and use it -- it will be much better.". Since Code::Blocks compiled and ran correctly previous tests with std::threads, I guessed it was fully compatible with C++11. - Tyrendel
AFAIK, Windows ports of gcc use the Windows port of pthreads to implement std::thread. That's why you need the -pthread switch when compiling. The command lines above show that you're only passing -lpthread to the link stage. Try adding -pthread to the compilation stage also (and probably do the same for the link stage; -pthread implies -lpthread) - Praetorian

1 Answers

0
votes

I was using TDM-GCC-481 with SFML compiled for TDM-GCC-471 (the one available for download).

I tried to install TDM-GCC-471 but it wasn't recognizing std::thread.

So I recompiled SFML with TDM-GCC-481 (very easy to do thanks to SFML tutorial and cmake) and now the bug is gone.