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

Author Topic: Problem with sf::sleep (freeze)  (Read 3047 times)

0 Members and 1 Guest are viewing this topic.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Problem with sf::sleep (freeze)
« on: February 19, 2014, 11:39:05 am »
Hello,

For some reason using an sf::sleep with an std::cout and no std::endl makes my program freeze forever:

#include <iostream>
#include <SFML/System/Sleep.hpp>

int main(int argc, char ** argv)
{
    while(true)
    {
        std::cout << "here";
        sf::sleep(sf::seconds(0.5f));
    }

        return 0;
}
 

However if I remove sf::sleep or if I add std::cout << "here" << std::endl; it works just fine. In addition, this was compiled in Ubuntu 12.04 (64-bit) with g++ 4.6.3, but for some reason the problem doesn't seem to happen in Windows (compiled with mingw/g++ 4.8 ).

What could be the problem here?

Thanks in advance,
Best regards

EDIT: Adding a std::cin.ignore(std::numeric_limits<int>::max(),'\n'); also seems to fix the problem for a while. This leads me to believe it could be a buffer problem? But why does sf::sleep affect this?

EDIT: Adding an std::flush also seems to work.
« Last Edit: February 19, 2014, 12:09:12 pm by Contadotempo »

Hapax

  • Hero Member
  • *****
  • Posts: 3054
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Problem with sf::sleep (freeze)
« Reply #1 on: February 19, 2014, 12:05:03 pm »
#include <iostream>
#include <SFML/System/Sleep.hpp>

int main(int argc, char ** argv)
{
    while(true)
    {
        std::cout << "here";
        sf::sleep(sf::seconds(0.5f));
    }

        return 0;
}
 
This code works fine for me. I don't think you should ever be using "while(true)" though :p
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problem with sf::sleep (freeze)
« Reply #2 on: February 19, 2014, 12:23:40 pm »
Well, a while(true) with a sf::sleep inside is the definition of a program that freezes forever, so you shouldn't be surprised of the result ;)

I think your program runs as expected, but std::cout outputs nothing until you use std::endl or std::flush. The behaviour depends on the platform: some may buffer text forever, some others may output text after a while, etc.
Laurent Gomila - SFML developer

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Re: Problem with sf::sleep (freeze)
« Reply #3 on: February 19, 2014, 12:31:31 pm »
I think your program runs as expected, but std::cout outputs nothing until you use std::endl or std::flush. The behaviour depends on the platform: some may buffer text forever, some others may output text after a while, etc.
Ah I see. I was confused because it did output the "here" string on windows but not on Linux. Probably didn't explain that too well. In that case I'm more relieved.

Thanks,
Best regards
« Last Edit: February 19, 2014, 12:33:48 pm by Contadotempo »