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

0 Members and 1 Guest are viewing this topic.

#### sfmlearner

• Newbie
• Posts: 9
##### sf::sleep(sf::microseconds()) does not stop under 999 microseconds
« on: April 14, 2022, 05:02:15 pm »
Hello,
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
sf::sleep(sf::microseconds(timeSleep));
}
}

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.

ty!

#### eXpl0it3r

• SFML Team
• Hero Member
• Posts: 10226
##### 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
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

#### kojack

• Full Member
• Posts: 170
##### 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.

anything