17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

129<br />

analogous to database transactions for controll<strong>in</strong>g access to shared<br />

memory <strong>in</strong> concurrent comput<strong>in</strong>g. It functions as an alternative to lockbased<br />

synchronization, <strong>and</strong> is typically implemented <strong>in</strong> a lock-free way. A<br />

transaction <strong>in</strong> this context is a piece of code that executes a series of reads<br />

<strong>and</strong> writes to shared memory. These reads <strong>and</strong> writes logically occur at a<br />

s<strong>in</strong>gle <strong>in</strong>stant <strong>in</strong> time; <strong>in</strong>termediate states are not visible to other<br />

(successful) transactions.<br />

The Magic Ball<br />

To use a bit of illustration, lets say we had a baseball that I wanted to get signed by<br />

every member of the 1949 Yankees. It’s a very special ball <strong>in</strong> that I can make copies on<br />

dem<strong>and</strong> <strong>and</strong> h<strong>and</strong> them out to whoever I would like. However, I am not giv<strong>in</strong>g them a<br />

permanent copy, I am just lett<strong>in</strong>g them sign it <strong>and</strong> h<strong>and</strong> it back. So ten people come up<br />

<strong>and</strong> ask to sign it. I magically make ten copies of the clean new ball. The fifth person I<br />

gave a ball too happens to get back to me the quickest. I replace the ball <strong>in</strong> my h<strong>and</strong> with<br />

the new ball number 5 just gave me. A few seconds later number 6 comes back hav<strong>in</strong>g<br />

signed his ball. I look at the ball <strong>in</strong> my h<strong>and</strong> <strong>and</strong> the ball he is giv<strong>in</strong>g to me <strong>and</strong> notice<br />

that they are different. My ball has number 5s signature, the ball he is giv<strong>in</strong>g me has<br />

number sixes signature. Uh oh, that’s not go<strong>in</strong>g to work. So I throw away number 6’s ball<br />

<strong>and</strong> make a copy of the ball I currently have <strong>in</strong> my h<strong>and</strong> <strong>and</strong> h<strong>and</strong> it back to number 6<br />

tell<strong>in</strong>g him to sign it. This repeates over <strong>and</strong> over aga<strong>in</strong> as the rest of the signers come<br />

back hav<strong>in</strong>g signed an out of date ball. In essence here, I am the gatekeeper <strong>and</strong> I make<br />

sure that I always have the most recent ‘copy’ of the ball.<br />

STM has a few benefits that aren't immediately obvious, from this<br />

description. First <strong>and</strong> foremost STM is optimistic. Every thread does what it needs<br />

to do without know<strong>in</strong>g or car<strong>in</strong>g if another thread is work<strong>in</strong>g with the same data.<br />

At the end of a manipulation, if everyth<strong>in</strong>g is good <strong>and</strong> noth<strong>in</strong>g has changed, then<br />

the changes are committed. If problems or conflicts occur the change can be rolled<br />

back <strong>and</strong> retried. The cool part of this is that there isn't any wait<strong>in</strong>g for resources.<br />

Threads can write to different parts of a structure without shar<strong>in</strong>g any lock. The<br />

bad part about this is that you have to retry failed commits. There is also some, not<br />

<strong>in</strong>significant, overhead <strong>in</strong>volved with the transaction subsystem itself that causes a<br />

performance hit. Additionally <strong>in</strong> certa<strong>in</strong> situations there may be a memory hit, ie if<br />

n processes are modify<strong>in</strong>g the same amount of memory you would need O(n) times<br />

memory to support the transaction. In general, for the programmer, this approach<br />

is many times more manageable than the ma<strong>in</strong>stream shared memory approach<br />

<strong>and</strong> may be a useful way of tak<strong>in</strong>g advantage of concurrency. We still consider this<br />

approach to be shared memory at its core but must admit that the position is an<br />

ongo<strong>in</strong>g argument between us <strong>and</strong> a number of our colleagues.<br />

5.1.4 Dataflow - Futures <strong>and</strong> Promises<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!