Elegance in Asynchrony


 

It’s been a long time since I’ve put finger to keyboard and
typed out a post on this here Wayward Weblog, an artifact of the ancient web
1.1, where men were men and posts were posts, and men posted nonsense into the amorphous
ether of the intertubes, hoping beyond hope that somehow, somewhere, someone
out there might follow its link and indulge in the inanity of a lone stranger’s
train of thought.  You might have wondered what happened.  Did the Wayward drop off the planet?  Or did he emigrate to the same future that
has so captivated the rest of the known universe? Did he switch to Twitter? Is
there a feed out there that encompasses the boldness that once was The Weblog?  Or did he just switch to Facebook and is only
posting sumptuous morsels of enlightenment to his vast inner circle of friends?
And why weren’t you friended? Who does this guy think he is?

 

No need to fret. Nothing so sinister has come to pass. You
would indeed all be friended by the Great Wayward himself had he the nerve to
actually book his own face, or the chutzpah to become one of the droves of twitterpaters
drowning the web in scrawls of cryptic blurbs and emoticons.  But alas, a non-social cretin like the
Wayward does not change his habits even when granted a tech-laden alternative
interface to reality. The world of hyper-salivating coolness that has overtaken
so many has simply passed him by.

 

The real truth is that I’m so anti-social that without constant
reminder that the world outside my office exists, I tend to forget everything
about it. It used to take extreme doses of overly caffeinated beverages to perk
my interest in the outer realms, the hallways and other peoples’ doors, something
that would give me a buzz and bad case of the shakes, anxiously rousing me from
my chair and sending me wandering dazedly around the building, frenetically bumping
into walls, tripping over chairs. Sometimes, if the planets aligned and the
conditions were just right, I might have found myself interacting with actual other
people. Back in the time of Jolt cola I got so wired I blacked out. I’m not
really sure what happened, though I did get written up on a HR violation. The
lawyers won’t let me talk about it anymore. Needless to say, those black days
are gone and will never be repeated; especially since I’ve been off the caffeine.

 

So you can see, it’s really not you, it’s me, honestly. Without
the personality altering drugs, I’m hopelessly lost in the only bit of reality
that captivates me; my code. Without survival instinct buried deep within my
brain and, of course, hunger signals I would not know when to leave and go
home.  It would just be my code, day in
day out, my internal world of organizing bits into blobs, refactoring hacks
into tweeks and spewing forth all that is holy, the unquestioned word of the
Wayward.

 

Which makes it extremely out of the ordinary for me be
writing this now, far outside my comfort zone. Something tripped the switch and
sent me mousing outside my code editor and into the bleak wasteland of the syntactically
challenged word processor to type up this non-executable diatribe. You see,
something extraordinary happened. Something so beyond fantastic, I could no
longer stay focused on the screen of scintillating source in front of me.  


As it turns out, and to my utter astonishment, somebody else’s
code was better.

 

And not just a little bit better; gobstoppingly better, from
coding cowboy coolness to cold cognitive bliss.

 

Last week at the PDC we released the Async CTP (community
technology preview) for C# and VB.  It’s
got this really nifty feature that turns otherwise horrendous gobbledygook of
callback nastiness into nice legible, linear source code, so you can actually
write asynchronous code as if it were synchronous; no muss, no fuss.

 

But I don’t really need to tell you about this, as you’ve
certainly read umpteen other blogs and news articles already that have either
confounded you with a bazillion examples or threatened you with what the code
would have otherwise looked like, the kinds of software geek horrors reserved
for Halloween night obfuscation contests.

 

We’ve all been there, right?

 

Of course, this wasn’t news to me.  I had been in the thick of it for a long
time.  Talking about asynchronous programming
was always just enough inducement to get me out from in front of my PC and into
a conference room.  It wasn’t the asynchrony
itself or the cutesy new keywords, or the magic rewrite that’s kind of like
iterators and kind of not.  It wasn’t the
stack spilling or intricate details of exception handling that lead me to this
degree of engagement.

 

It wasn’t the async feature at all.  It was one of the samples.

 

You see, while I had been long involved in the design I’d
never much paid attention to the periphery. 
Until the CTP released, I had never took much notice in the samples the
others were busily compiling. 


Yet when I did, I found one sample that completely embodied
the elegance and beauty of the underlying asynchrony itself.  It was and is so profoundly tied to the deep
concept that I don’t think any other example could ever match it.

 

I’m sure you are oozing with curiosity so I’ll let you in on
the secret.  It’s the ‘Dining Philosophers’
sample. Go ahead and pull that one up out of the CTP and take a look at the
source.  I’ll wait.  You’ll probably see where I’m coming from and
where I’m going just by ogling the text, but let me explain anyway.

 

The Dining Philosophers problem is a classic computer
science problem that involves finding a process by which a group of
self-serving windbags can all sit down at a table and share a meal without
anyone starving. You’d think that such highly educated folks would not need help
in such a matter, but in fact they do, since they are seldom in agreement and
are often focused on talking more than anything else that they seldom take the
time to even order their food, let alone realize there are not enough forks to
go around. Eggheads like me find it a grand pastime to imaging different
variations of forcing the gaggle to agree on how to accomplish this and there
are many interesting and elegant solutions.

 

For me, however, the asynchronous sample is by far the most
interesting and the most elegant.  It’s
not because the new language feature makes the code vastly simpler than it
would otherwise have been, allowing it to please the eyes and hearts of
software enthusiasts of all ilk.  The
true elegance is not even readily apparent in the source itself.  It’s more both at a meta level and an
implementation level that the magnificence of the asynchrony feature really
shines through.  You might not even notice
it unless you have the kind of background in both the Dining Philophers problem
and the CTP that I have, so I forgive you if you are still puzzled at this
point.

 

You see, the compiler rewrites the cutesy new asynchrony operator
into a few more explicit operations that harness the true power.  You will see it right away if and when you
either decode the IL or manually translate it in your head.  The very first thing that happens is the
compiler generates a call to ‘GetAWaiter’, and this alone has an immediate and
dramatic impact for the dining philosophers. 


After all, once you’ve gotten a waiter, the problem about
too few forks simply solves itself.

 

I’ll go back to my code now.

 

Comments (7)

  1. Blake says:

    *groan*

    Miss the old posts, but yes, that was funny in a horrible way.

  2. Barry Kelly says:

    That really is an atrocious joke!

    As to the sample, it is merely the careful ordering of fork selection that prevents the deadlock (i.e. one philosopher tries to pick up a different fork than the one to the left of them first); there's nothing about the async support that helps prevent it if the forks are handed out in the more traditional fashion (i, (i+1) % count), and indeed if the sample is modified to use this approach, so that every philosopher picks up their left fork before their right, the system can deadlock if the wait between picking up each fork happens to be long enough.

    Also notable in the sample is how it demonstrates concurrency but not parallelism, as every continuation resumes in the UI thread.

  3. Wagneripjr says:

    Cool! 🙂

  4. leniel says:

    Ha ha ha 😀

  5. Matt Warren says:

    Nice article, glad you're blogging again.

  6. You have no idea how long I've been holding onto that one!

  7. Yes, the actual sample does not correctly solve the problem.  For that you'd need to have some sort of asynchronous multi-lock.  But pointing that out would have spoiled the joke.

Skip to main content