I would like to execute any function with the simple defined signature
func() with a defined tmieout. So my current approach is
func ExecInTime(f func(), ns int64) bool {
inTimeChan := make(chan bool)
// Wrapper function.
wf := func() {
f()
inTimeChan <- true
}
// Timeout function.
timeout := func() {
time.Sleep(ns)
inTimeChan <- false
}
// Now do it and see who wins.
go wf()
go timeout()
return <-inTimeChan
}
So far so good. But if my timeout func wins the original function
naturally continues working in the background. So I would like to kill
it (would be the hard way) or just a better solution. A communication
based solution inside the function f() is btw. not possible.
mue
- Mike
> So far so good. But if my timeout func wins the original function
> naturally continues working in the background. So I would like to kill
> it (would be the hard way) or just a better solution. A communication
> based solution inside the function f() is btw. not possible.
Currently is no way to kill a goroutine, other than sending it a
message over a channel.
Ian
Then no, there's no way to do it.
Killing individual goroutines is a very unstable thing to do:
it's impossible to know what locks or other resources those
goroutines had that still needed to be cleaned up for the
program to continue running smoothly. I don't expect that
ability any time soon.
Russ
Thx
mue
Check the channel once in a while to see if it's time to stop.
Russ
mue
> OK. But how to check a channel without blocking? A kind of "peek".
http://golang.org/doc/go_spec.html#Communication_operators
v, ok = <-c
Ian
So my fun developing in Go can go on. Another plus for my CeBIT talk.
*smile*
mue
Once again thx.
mue