I’m trying to figure out how to use enif_consume_timeslice, and I don’t feel like I’m understanding it.
while (true) {
// ... do expensive work here, return result if computation completes
if (enif_consume_timeslice(env, percent)) {
return enif_schedule_nif(...);
}
}
Is this how this works? How am I supposed to calculate percent? Should I literally time how long each loop iteration takes and divide that by one millisecond?
It is to be used as a best effort guesstimate. What you want to avoid is the expensive work taking seconds without re-scheduling. If you don’t have a good way of knowing how much time the “do expensive work” can take, then it might be a better idea to just run it on a dirty CPU scheduler.