I need my threads to regularly wake up during wait periods to e.g. send a regular keep-alive signal to the server, or to timeout a network request initiated by another thread.
How would I do this with the new interface? I really don’t want to resort to a busy-wait or sleeping.
1 Like
Would you mind opening an issue for this? I just confirmed indeed std.Thread.Semaphore and std.Thread.Condition each had a timedWait, and the new Io.Condition and Io.Semaphore should have a timedWait as well.
1 Like
I have this PR open for a while
1 Like
Sorry that slipped under the radar for me, I wasn’t ignoring it intentionally
I’m using a heartbeat in my telemetry worker, that works pretty well:
fn telemetryLoop(self: *Worker) void {
const max_age_ms: u64 = 3000;
const heartbeat_interval_ms: u64 = 5e3;
var threaded: std.Io.Threaded = .init(self.gpa, .{ .environ = .{ .block = .empty } });
defer threaded.deinit();
const io = threaded.io();
while (!self.shutdown.load(.acquire)) {
const now_ms = timer.timestamp(io);
// send heartbeat
if (now_ms - self.last_heartbeat_ms >= heartbeat_interval_ms) {
const sensor = Sensor{
.uri = self.uri_heartbeat,
.serial_number = self.name,
.range = self.range,
.latitude = self.latitude,
.longitude = self.longitude,
};
http_sender.sendHeartbeat(io, self.gpa, sensor);
self.last_heartbeat_ms = now_ms;
}
// send event data
if (self.ring.pullFirstValid(io, max_age_ms)) |ev| {
http_sender.sendEventData(io, self.gpa, self.name, ev);
} else {
timer.sleepMillis(io, 1);
}
}
}