Date post: | 30-May-2018 |
Category: |
Documents |
Upload: | don-stewart |
View: | 217 times |
Download: | 0 times |
of 13
8/14/2019 Evaluation strategies and synchronization: things to watch for
1/13
2008 Galois, Inc. All rights reserved.
Lazy Synchronization MethodsThings to watch out for
Don Stewart | Galois Dev Symposium | Feb 2010
8/14/2019 Evaluation strategies and synchronization: things to watch for
2/13
2008 Galois, Inc. All rights reserved.
Control.Concurrent.*
A set of abstractions for threadcommunication and synchroniztion
Mvar synchronizing variables Chan unbounded channels
Simple, clean semantics makes
concurrency easy
8/14/2019 Evaluation strategies and synchronization: things to watch for
3/13
2008 Galois, Inc. All rights reserved.
But beware ofevaluation strategies!
putMVar fills an Mvar with a value
but doesn't force evaluation
so mostly you're just putting a pointer to aclosure in there
The thread 'take'ing the message gets the
nasty surprise of having to evaluate themessage! Work gets done in the wrongplace
8/14/2019 Evaluation strategies and synchronization: things to watch for
4/13
2008 Galois, Inc. All rights reserved.
Manual fixes
putMVar mv $! x + 1
ok for atomic types (one depth of
constructors)
Harder for normal forms:
let x' = x + 1 in rnf x `seq` putMVar mv x
8/14/2019 Evaluation strategies and synchronization: things to watch for
5/13
2008 Galois, Inc. All rights reserved.
Same risks hold for...
IORef
Mvar
Chans (lists of Mvars)
Semaphores...
Data.Map.insertWith...
8/14/2019 Evaluation strategies and synchronization: things to watch for
6/13
2008 Galois, Inc. All rights reserved.
More insidious...
modifyMVar considered harmful!
ex1.hs
8/14/2019 Evaluation strategies and synchronization: things to watch for
7/13
2008 Galois, Inc. All rights reserved.
Seen in the wild...
Seen in a production system
newUnique (US ref) = atomicModifyIORef ref $ \u ->let !u' = u+1 in (u', Unique u)
This catches even the best programmers!(And worse with the atomic* functions)
8/14/2019 Evaluation strategies and synchronization: things to watch for
8/13
2008 Galois, Inc. All rights reserved.
One way to fix it...
ex2.hs
mean 68.3ms, 451us std dev.
8/14/2019 Evaluation strategies and synchronization: things to watch for
9/13
2008 Galois, Inc. All rights reserved.
A big hammer
RNF-by-default for synchronizationvariables!
cabal install strict-concurrency First library I wrote at Galois. :-)
ex3.hs
8/14/2019 Evaluation strategies and synchronization: things to watch for
10/13
2008 Galois, Inc. All rights reserved.
Timings
mean: 61.7ms, 2ms std dev!
8/14/2019 Evaluation strategies and synchronization: things to watch for
11/13
2008 Galois, Inc. All rights reserved.
importControl.Concurrent.MVar.Strict
ex4.hs
Raises the issue: rnf can be very heavy (atraversal on every commit!)
Often wasting work. Need an 'rnf' bit...
(see Andy Gill's proposal)
8/14/2019 Evaluation strategies and synchronization: things to watch for
12/13
2008 Galois, Inc. All rights reserved.
Lessons
Be careful with modify* functions
Be careful with lazy synchronization
work gets migrated around
In general,
always use putMVar mv $!
always use strict channels, or manuallyevaluate before putting into the channel
8/14/2019 Evaluation strategies and synchronization: things to watch for
13/13
2008 Galois, Inc. All rights reserved.
Addtionally...
block/unblock on exceptions can be reallysubtle.
Be careful! And threadDelay (or other thread stopping
methods) complicates block.
Be careful with async messages +synchronization!