On Tue, Apr 11, 2017 at 6:09 PM, Alex Flint <alex.fl...@gmail.com> wrote:
>
> Suppose I have a long-running computation that I wish to be cancelable via a
> context. By this I mean something that's just churning away on some
> computation, not blocking on a syscall or I/O operation or anything like
> that. Should I just poll ctx.Err() periodically in the inner loop of my
> computation?

Technically you should poll ctx.Done, as ctx.Err is not defined until
the channel returned by ctx.Done is closed.  But see
https://golang.org/issue/19856 .  Anyhow, that aside, yes, poll
periodically.

> Also, can anyone give any insight into approximately how costly
> context.Err() is for the contexts constructed in the context package -- I
> don't want my computation to be dominated by checking for cancellation!

Calling Done (or Err) for a cancellable context will acquire and
release a sync.Mutex.  Other than that they are very cheap.

Note that I wouldn't necessarily poll in the inner loop.  How often to
poll depends on what your program is doing.  For example, if it's a
background computation, then it doesn't matter much if it runs too
long, and polling in the next-to-inner loop would typically be
acceptable.

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to