2 ms·
In imperative languages you can safely deliver "asynchronous exceptions" only if you write your code asynchronously, i.e. in a form of asynchronous events. Even
by zzzcpan 11y ago
In imperative languages you can safely deliver "asynchronous exceptions" only if you write your code asynchronously, i.e. in a form of asynchronous events. Event loops usually deliver OS signals into event-driven programs in exactly this way. And it's always safe, because none of the event handlers can be interrupted or run in parallel, the whole program is designed with that assumption.
This is how asynchronous processes might look like:
func procA (cb func(error)) (func(error)){
var enter, something, leave func(error)
var shared int
enter = func (error err){
...
shared ++
...
waitForSomething (something)
}
something = func (error err){
if err != nil {
post (leave)
return
}
...
}
...
leave = func (error err){
...
post (cb)
}
return enter
}
It is also easy to preserve same guaranties and schedule such processes to multiple cores. Although to do that in Go you would need to explicitly pass a struct with all of the information about each process, its resources and its environments.