This month’s Effective Concurrency column, “Know When to Use an Active Object Instead of a Mutex,” is now live on DDJ’s website.
From the article:
Let’s say that your program has a shared log file object. The log file is likely to be a popular object; lots of different threads must be able to write to the file; and to avoid corruption, we need to ensure that only one thread may be writing to the file at any given time.
Quick: How would you serialize access to the log file?
Before reading on, please think about the question and pencil in some pseudocode to vet your design. More importantly, especially if you think this is an easy question with an easy answer, try to think of at least two completely different ways to satisfy the problem requirements, and jot down a bullet list of the advantages and disadvantages they trade off.
Ready? Then let’s begin.
I hope you enjoy it. Finally, here are links to previous Effective Concurrency columns:
1 The Pillars of Concurrency (Aug 2007)
2 How Much Scalability Do You Have or Need? (Sep 2007)
3 Use Critical Sections (Preferably Locks) to Eliminate Races (Oct 2007)
4 Apply Critical Sections Consistently (Nov 2007)
5 Avoid Calling Unknown Code While Inside a Critical Section (Dec 2007)
6 Use Lock Hierarchies to Avoid Deadlock (Jan 2008)
7 Break Amdahl’s Law! (Feb 2008)
8 Going Superlinear (Mar 2008)
9 Super Linearity and the Bigger Machine (Apr 2008)
10 Interrupt Politely (May 2008)
11 Maximize Locality, Minimize Contention (Jun 2008)
12 Choose Concurrency-Friendly Data Structures (Jul 2008)
13 The Many Faces of Deadlock (Aug 2008)
14 Lock-Free Code: A False Sense of Security (Sep 2008)
15 Writing Lock-Free Code: A Corrected Queue (Oct 2008)
16 Writing a Generalized Concurrent Queue (Nov 2008)
17 Understanding Parallel Performance (Dec 2008)
18 Measuring Parallel Performance: Optimizing a Concurrent Queue(Jan 2009)
19 volatile vs. volatile (Feb 2009)
20 Sharing Is the Root of All Contention (Mar 2009)
21 Use Threads Correctly = Isolation + Asynchronous Messages (Apr 2009)
22 Use Thread Pools Correctly: Keep Tasks Short and Nonblocking(Apr 2009)
23 Eliminate False Sharing (May 2009)
24 Break Up and Interleave Work to Keep Threads Responsive (Jun 2009)
25 The Power of “In Progress” (Jul 2009)
26 Design for Manycore Systems (Aug 2009)
27 Avoid Exposing Concurrency – Hide It Inside Synchronous Methods (Oct 2009)
28 Prefer structured lifetimes – local, nested, bounded, deterministic(Nov 2009)
29 Prefer Futures to Baked-In “Async APIs” (Jan 2010)
30 Associate Mutexes with Data to Prevent Races (May 2010)
31 Prefer Using Active Objects Instead of Naked Threads (June 2010)
32 Prefer Using Futures or Callbacks to Communicate Asynchronous Results (August 2010)
33 Know When to Use an Active Object Instead of a Mutex (September 2010)