CodeSOD: Synchronized Threads |
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: Its impossible, I used locks to synchronize the threads!
Well, he did use locks at the very least.
///
/// Performs the synchronisation
///
/// Current state
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.
[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!
| Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |