jwiegley / async-pool

Other
21 stars 13 forks source link

Document example usage of `mapReduce` (and any similar functions). #16

Open vdukhovni opened 4 years ago

vdukhovni commented 4 years ago

It was not immediately obvious to me how to construct a correct invocation of mapReduce. Its signature shows it returning an STM (Async a), so it seemed not too far-fetched to try to wait for it in the same STM transaction that created it:

withTaskGroup 8 \tg -> atomically (mapReduce tg tasklist >>= waitSTM)

but that fails with the dreaded "thread blocked indefinitely" error. It turns out (after finding an example in one of the tests) that the correct invocation is rather:

withTaskGroup 8 \tg -> atomically (mapReduce tg tasklist) >>= wait

With the waitSTM (via wait) in a separate atomic transaction. Some text showing correct usage may also be helpful to others, perhaps with an explanation motivating the need for the separation (but a simple dictate may suffice).

jwiegley commented 4 years ago

Excellent idea.