kazu-yamamoto / dnsext

Extensible DNS libraries written purely in Haskell
59 stars 3 forks source link

Separate print result #176

Closed kazu-yamamoto closed 4 months ago

kazu-yamamoto commented 5 months ago

Using STM log for atomicity.

khibino commented 4 months ago

Multiple threads may execute printResultSTM with the same result, like following event sequence:

By putTMVar from the thread that outputs the results, make others wait.

https://github.com/kazu-yamamoto/dnsext/commit/fe0a206f3479321682cf0ca326e0952340add18e

kazu-yamamoto commented 4 months ago

Ah! Thanks. Let's merge.

kazu-yamamoto commented 4 months ago

thread1: read False from tvar thread2: read False from tvar thread1: run printResultSTM thread2: run printResultSTM thread1: write True to tvar thread2: write True to tvar

This never happens because STM actions run atomically. Reverting fe0a206.

khibino commented 4 months ago
thread1: read `False` from tvar
thread2: read `False` from tvar
thread1: run `printResultSTM`
thread2: run `printResultSTM`
thread1: write True to tvar
thread2: write True to tvar

Sorry. When thread2 does printResultSTM, it knows it broke thread1's transaction, so thread2 retries. So the original program is safe.