abseil / abseil/abseil-cpp

Add SleepUntil

未關閉
#1,088 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
bug
主要語言
C++
星號
18.1k
分支
3.2k
平均合併
20 小時 36 分鐘
30 天內合併 PR
1

描述

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.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。