SFML community forums

Help => System => Topic started by: b3ngr33ni3r on December 12, 2011, 12:40:22 am

Title: sf::Thread and cin
Post by: b3ngr33ni3r on December 12, 2011, 12:40:22 am
Hey guys, quick question. i'm trying to get user input while still running another thread (this other thread is drawing a sf::RenderWindow). however, when attempting to cin in the thread, it pauses the entire program, causing the sf::RenderWindow to freeze up. So, i'm wondering how to get userinput in a thread while not stopping the main program (i'm guessing i can't use cin)


Thanks.
Title: sf::Thread and cin
Post by: Laurent on December 12, 2011, 07:39:42 am
You're probably doing something wrong, using std::cin in a thread shouldn't block other threads.
Title: sf::Thread and cin
Post by: b3ngr33ni3r on December 12, 2011, 05:20:08 pm
Okay, would you mind taking a look?

The pertinent info is Init() which calls Listener() where Listener() tries to std::cin.

https://github.com/b3ngr33ni3r/Magnet/blob/experimental/Console/Console.cpp

https://github.com/b3ngr33ni3r/Magnet/blob/experimental/Console/Console.h
Title: sf::Thread and cin
Post by: RaptorIV on December 12, 2011, 06:40:50 pm
That is odd, it seems as though cin is pausing the thread, but it shouldn't
Title: sf::Thread and cin
Post by: Laurent on December 12, 2011, 08:10:17 pm
Your sf::Thread instance is destroyed as soon as the thread is launched (because it is local), and the destructor of sf::Thread calls Wait(). So it's exactly as if you called the Console::Listener function directly. The sf::Thread instance must be alive as long as the thread runs ;)
Title: sf::Thread and cin
Post by: b3ngr33ni3r on December 12, 2011, 10:20:43 pm
Ah, Perfect. All set now. Thanks!