abseil / abseil/abseil-cpp

Add SleepUntil

Offen
#1,088 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
C++
Sterne
18.1k
Forks
3.2k
Ø Merge
20 Std. 36 Min.
Gemergte PRs (30 T.)
1

Beschreibung

Feature request: It would be great if Abseil exposed a `SleepUntil` function alongside `SleepFor`. A Linux implementation could be something like this:

```c++
// Copyright 2022 Benjamin Barenblat
// SPDX-License-Identifier: Apache-2.0

// SleepUntil()
//
// Sleeps until the current time is after the specified absolute time.
//
// Notes:
// * Signal interruptions will not reduce the sleep duration.
// * Returns immediately when given a time that has already passed.
void SleepUntil(absl::Time time) {
struct timespec wake_point = absl::ToTimespec(time);
while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &wake_point,
/*remain=*/nullptr) == EINTR) {
// Ignore signals and wait for the full interval to elapse.
}
}
```

This obviously won’t work on Windows, but I believe you can use [`SetWaitableTimer`](https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-setwaitabletimer) to achieve something similar. `clock_nanosleep` doesn’t exist on Apple OSes either; I’m not sure what the correct approach is there.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.