Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Thread and cin  (Read 4916 times)

0 Members and 1 Guest are viewing this topic.

b3ngr33ni3r

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::Thread and cin
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Thread and cin
« Reply #1 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.
Laurent Gomila - SFML developer

b3ngr33ni3r

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::Thread and cin
« Reply #2 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

RaptorIV

  • Newbie
  • *
  • Posts: 30
    • View Profile
sf::Thread and cin
« Reply #3 on: December 12, 2011, 06:40:50 pm »
That is odd, it seems as though cin is pausing the thread, but it shouldn't

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Thread and cin
« Reply #4 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 ;)
Laurent Gomila - SFML developer

b3ngr33ni3r

  • Newbie
  • *
  • Posts: 6
    • View Profile
sf::Thread and cin
« Reply #5 on: December 12, 2011, 10:20:43 pm »
Ah, Perfect. All set now. Thanks!