Most active commenters

    ←back to thread

    115 points saisrirampur | 11 comments | | HN request time: 0.833s | source | bottom
    1. gsliepen ◴[] No.44568872[source]

      pg_usleep(1000L);
    
    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.

    replies(4): >>44569067 #>>44570238 #>>44571006 #>>44571476 #
    2. delusional ◴[] No.44569067[source]
    > 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.

    replies(1): >>44569435 #
    3. dwattttt ◴[] No.44569435[source]
    Spurious wakeups enters the chat.
    replies(1): >>44569882 #
    4. quietbritishjim ◴[] No.44569882{3}[source]
    Any reasonable sleep API will handle spurious wake ups under the hood and not return control.
    replies(1): >>44569909 #
    5. Joker_vD ◴[] No.44569909{4}[source]
    It will also ignore those pesky signals and restart the sleep :)

    On a more serious note, any reasonable sleep API should either report all wake ups, no matter the reason, or none except for the timeout expiration itself. Any additional "reasonable" filtering is a loaded footgun because different API users have different ideas of what is "reasonable" for them.

    replies(1): >>44570473 #
    6. anarazel ◴[] No.44570238[source]
    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)

    7. ◴[] No.44570473{5}[source]
    8. OptionOfT ◴[] No.44571006[source]
    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.
    9. aspbee555 ◴[] No.44571476[source]
    I was working through some ffmpeg timing stuff and the ai kept insisting on putting in sleep functions to try and "solve" it
    replies(1): >>44571550 #
    10. jeffbee ◴[] No.44571550[source]
    Generative coding models are trained on GitHub. GitHub is completely full of inadvisable code. The model thinks this is normal.
    replies(1): >>44572350 #
    11. nightfly ◴[] No.44572350{3}[source]
    Sort of by definition it is normal