Overview/Examples/6. Staying inside a time budget
worked example 6 of 6
Staying inside a time budget
A Progress callback returning false cancels cleanly, leaving a valid intermediate mesh, so a long operation can be bounded by wall-clock time rather than by guesswork.
Example 6 of six - the program sits on the examples page, and this page takes it from the running end: the console it produces, the Status values that end it early, and the options worth changing.
What it prints
stage quadrics 8%
stage collapse 61%
cancelled at 1.50 s
status: Cancelled
triangles now 214900 (partial)
A cancelled decimation has done fewer collapses, not an invalid half-collapse; the mesh it leaves is smaller than the input and entirely usable.
When it goes wrong
- Thinking cancel means failure
Status::Cancelledis a normal outcome, not an error; the mesh is valid and the caller decides what to do with the partial result.- A non-thread-safe callback
- The callback is invoked from the calling thread only, even with
threadsabove one, so it needs no locking - but do not assume it fires on a worker. - Comparing stage text by pointer across builds
stageis a literal, safe to compare by pointer within one build; compare the text if the pointer might cross a library boundary.
Variations
- Report, do not cancel
- Return
truealways and use the callback purely to drive a progress bar. - Cancel on a signal
- Check an atomic flag inside the callback so an operator can stop a batch job between about every two percent.