Friday, September 29, 2006

Beware the concurrency monster

Just when you think you know it, you relax a little and the concurrency monster is back with a vengeance! This is a true story...

Once upon a time there lived a lecturer who taught concurrent programming. After years of training and practice he wandered the concurrent landscape with pride and confidence. His offerings to the programming gods had granted him safe travel, and he ventured forth with his new batch of students aiming to get further into the landscape than ever before.

The adventure was going well and one night the lecturer started examining the progress of his young apprentices. While many were doing well they needed a small challenge, something to test the tools they had created. Their latches, semaphores, barriers and exchangers all looked promising, but had remained untested.

"The end of the trek is near, so the challenge must be quickly created." the lecturer thought to himself as he studied their tools. Consulting the dark concurrency forces, he searched for a suitable challenge. Using a cauldron of dread he combined Wing of Interrupt, with Heart of Thread, and a touch of Join dust. In his rush he failed to see the beast that was being created... All day and all night the potion brewed, and on the following day he offered it to his students.


Each student took the potion without question, after all he was the lecturer. The potion was slow, allowing the lecturer to depart before it stirred. Initially the tools held the beast at bay, the Latch stood strong, the Semaphore withheld... then the Barrier was broken, the Exchanger collapsed, and the tools began to fall one after the other. Valiantly the students poured over their code, inspecting it line by line, trying to find the cause of this beast. Their efforts were misguided, none could withstand the beast for it came from the potion, not their code. The concurrency monster leapt from the lecturer's code and roared into existence.

The howling of the beast woke the lecturer with a jolt. Rushing to his students side the lecturer began looking for the cause of this mighty beast. Confident in his code he re-examined the students' code, but one after the other the code was safe. It handled the interrupts, and the many threads, but failed to meet at the join. There could only be one source for this beast... HIS code!

Stepping up to the code, the lecturer went one on one with the beast he had created. The beast was strong, and would not go down without a fight. Code was flying here and there as the beast and lecturer did battle. Minutes later the lecturer rose, covered in code, but triumphant. Returning the tamed test to the students, he stayed around to see if the beast woke again.

His caution was warranted as the second round of tests woke the very same beast. His battle had been in vain. Mustering the last of his strength the lecturer began to mind meld with the beast. Inspecting its structure and form. Searching for its weakness. Then it sprang to him. The timed methods, that can bring these tools back to life. With this new knowledge he reentered the fray and after a few hours had the beast cornered. A quick TryAcquire followed by TryExchange, and the beast went down begging for mercy. Then the final death blow, TryArrive.

With the beast gone (for now) the students returned to testing their utilities. The latches held, the semaphores counted, the barriers stayed strong, and the exchangers once again began to exchange. A cautious peace returned to the world of concurrency. The lecturer once again went back to roaming the concurrency landscape. But the scars from the battle causing him to limp slightly...

So when you next venture into the world of concurrency and pray to the Programming Gods, remember to make an offering to the concurrency monster... just in case.

5 comments:

Anonymous said...

Perhaps in the distant future when threading and CPUs are seen as barbaric, this tale will be regarded as today's "Brothers Grimm" stories are. :)

Unknown said...

Too damn quick to take that potion. I should have at least questioned what was in it :p

Anonymous said...

I loved the cautionary tale. I tend to avoid the concurrency monster where possible these days, but I do dream of returning to attempt to tame the beast.

Anonymous said...

Drink the CoolAid.

DRINK THE COOLAID!

Anonymous said...

That's awesome to listen to.
Even the Great Andrew Cain is not invincible to the dreaded threaded app.

It is true homage to his words. If you think you have it mastered, then you know very little. (Not that I claim A.C. knows little)