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

Author Topic: sf::sleep(sf::microseconds()) does not stop under 999 microseconds  (Read 3118 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 9
    • View Profile
I have a function to sort a vector, the function is inside a while loop that loops until the vector is sorted.

Here is the function:
void bubbleSort(std::vector<Sortable>& sortElements, int timeSleep) {
                for (int n = 0; n < sortElements.size() - 1; n++) {
                        if (sortElements[n].value > sortElements[n + 1].value) {
                                // Swap positions
                                auto currElement = sortElements[n];
                                auto tempElement = sortElements[n + 1];
                                sortElements[n + 1] = currElement;
                                sortElements[n] = tempElement;

                        // Wait timeSleep microseconds between iterations

I wanted to implement a time sleep between each cycle, works good above 1000 microseconds (1ms), I also tested if there is any difference in time between 1000 and 1500 microseconds and there is a difference in time, so everything works great for now. But, when timeSleep is 999 or less, the time that each cycle takes is the same, from 1 to 999 microseconds (just like the sleep being skipped).

I also tried this alternative, but does something similar, under a certain range just rounds up to n miliseconds:

The function is being executed in a detached thread.



  • SFML Team
  • Hero Member
  • *****
  • Posts: 10895
    • View Profile
    • development blog
    • Email
Re: sf::sleep(sf::microseconds()) does not stop under 999 microseconds
« Reply #1 on: April 14, 2022, 09:09:19 pm »
sf::sleep is mostly a hint to the OS. Whether it actually performs a sub 1ms sleep for a thread, is up to the OS. The precision of the sleep also depends on the OS and can be not as accurate.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
Dev Blog: https://duerrenberger.dev/blog/


  • Sr. Member
  • ****
  • Posts: 329
  • C++/C# game dev teacher.
    • View Profile
Re: sf::sleep(sf::microseconds()) does not stop under 999 microseconds
« Reply #2 on: April 15, 2022, 12:22:45 am »
On Windows, sf::sleep's implementation calls the Win32 Sleep function, which takes an integer number of milliseconds. So it can't go below 1ms.