Article 2Y4YN CodeSOD: Synchronized Threads

CodeSOD: Synchronized Threads

by
Remy Porter
from The Daily WTF on (#2Y4YN)

Tim was debugging one of those multithreading bugs, where there appeared to be a race condition of some kind. The developer who had initially written the code denied that such a thing could exist: "It's impossible, I used locks to synchronize the threads!"

Well, he did use locks at the very least.

/// <summary>/// Performs the synchronisation/// </summary>/// <param name="state">Current state</param>private void Synchronize(object state){ // Take care that this can only run in one thread at a time var lockThis = new Object(); lock (lockThis) { //"code" }}

There is of course, one problem. The object you use for the lock needs to be shared across threads. This is less a "lock" in the sense of an "air lock" and more a lock in the sense of a "complete hull breach".

puppetlabs50.png[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today! TheDailyWtf?d=yIl2AUoC8zA_2_bxmiLTEU
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments