▲ | gsliepen 18 hours ago | ||||||||||||||||||||||||||||||||||
Virtually any time you put a fixed sleep in your program, it's going to be wrong. It is either too long or too short, and even if it is perfect, there is no guarantee your program will actually sleep for exactly the requested amount of time. A condition variable or something similar would be the right thing to use here.Of course, if this code path really is only taken very infrequently, you can get away with it. But assumptions are not always right, it's better to make the code robust in all cases. | |||||||||||||||||||||||||||||||||||
▲ | anarazel 13 hours ago | parent | next [-] | ||||||||||||||||||||||||||||||||||
The usleep here isn't one that was intended to be taken frequently (and it's taken in a loop, so a too short sleep is ok). As it was introduced, it was intended to address a very short race that would have been very expensive to avoid altogether. Most of the waiting is intended to be done by waiting for a "transaction lock". Unfortunately somebody decided that transaction locks don't need to be maintained when running as a hot standby. Which turns that code into a loop around the usleep... (I do agree that sleep loops suck and almost always are a bad idea) | |||||||||||||||||||||||||||||||||||
▲ | aspbee555 11 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||
I was working through some ffmpeg timing stuff and the ai kept insisting on putting in sleep functions to try and "solve" it | |||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||
▲ | OptionOfT 12 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||
If I were reviewing that code I would've asked if they tried yielding. Same effect, but doesn't delay when there is no one to yield to. | |||||||||||||||||||||||||||||||||||
▲ | delusional 17 hours ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||
> there is no guarantee your program will actually sleep for exactly the requested amount of time This is technically true, but in the same way that under a preemptive operating system there's no guarantee that you'll ever be scheduled. The sleep is a minimum time you will sleep for, beyond that you already have no guarantee about when the next instruction executes. | |||||||||||||||||||||||||||||||||||
|