What happens when the Async value is garbage collection?

Well ... - apparently nothing! If i try

Prelude Control.Concurrent.Async Data.List> do {_ <- async $ return $! foldl '(+) 0 [0,0.1 .. 1e + 8 :: Double]; print "Async is lost!" }
"Async is lost!"

one processor core starts to work for some time, the interface remains normal. Obviously, the thread starts and just starts until something needs to be done.

But (efficiency aside), that is, in principle, normal, or should Async always be either cancel led or wait ed for? Is something breaking because there is simply no way to read the result? And does the GC cleanse properly? Perhaps the thread will really stop, and it just won’t happen when I try (due to lack of memory pressure)? Does the thread really even β€œend” at all, simply when the forkIO ed action forkIO ?

I am not at all sure about this concurrency stuff. Maybe I'm still thinking too much about it in C ++. RAII / deterministic garbage collection certainly makes you feel a little better in such ways ...

+6
source share
1 answer

Internally, Async is a Haskell stream that is written to STM TMVar upon completion. A cancel just sends the Haskell stream to the kill signal. In Haskell, you don't need to kill threads abruptly. If Async itself can be garbage collected, the stream will still work to the end, and then everything will be cleaned up correctly. However, if Async ends in an exception, then wait will propagate the exception to the waiting thread. If you do not wait , you will never know that an exception has occurred.

+6
source

Source: https://habr.com/ru/post/959390/


All Articles