Dr. Dobb's Journal - November 2007 - (Page 59) d11sutt_p6ma 9/10/07 11:29 AM Page 59 one thread before the synchronization, and another thread after the synchronization), and there isn’t enough synchronization here to do that. Let’s see why. The problem is that this anti-idiom is trying to abusively invert the usual meanings of a locked section. Thread 2 is abusively trying to use Thread 1’s entering a critical section as a release event, which it isn’t. Remember, entering a critical section is an acquire event, and leaving a critical section is a release event. On this, the whole world depends, as we saw last month [1]. In particular, compiler and processor optimizations are guaranteed to respect normal critical section boundaries, and therefore not change the semantics of correctly synchronized code; specifically, they respect the rule that code can be reordered to move into, but not out of, a critical section. In the context of Thread 1, that means an optimizer or processor is free to actually execute line a after line b… and therefore, Thread 2 could well see a value of 0 for x, despite its attempts to abuse the global visibility of locking. In reality, the code may happen to work all the time on a given system that doesn’t happen to reorder lines a and b. The original programmer might be long gone by the time some other poor sod tries to port it to a new compiler or platform that does manifest the race, and gets to experience the joy of debugging the intermittent problem over a holiday weekend. (Note that, besides the issues we’ve discussed, this code has other potential problems; for example, Thread 2 can wait forever if Thread 1’s lock is taken too early.) Treat a critical section as you would treat another person: Turning either inside out is at least cruel, and usually illegal. For all the sensational, lurid, and unsavory details, see [3]. ordered atomic variable; and exit a critical section by releasing a lock or writing to an ordered atomic variable. Never pervert these meanings; in particular, don’t abuse trylock to (try to) make a lock acquire in another thread act like the end of a critical section. Notes [1] H. Sutter. “Use Critical Sections (Preferably Locks) to Eliminate Races,” Dr. Dobb’s Journal, October 2007. [2] H. Sutter. “The Trouble With Locks,” C/C++ Users Journal, March 2005. Available at http://gotw.ca/publications/mill36.htm. [3] H. Boehm. “Reordering Constraints for Pthread-Style Locks,” Proceedings of the 12th ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming (PPoPP’07), March 2007. DDJ Herb is a software architect at Microsoft and chair of the ISO C++ Standards committee. He can be contacted at www.gotw.ca. Summary Apply critical sections consistently to protect shared objects: Enter a critical section by taking a lock or reading from an Make Networked and Multithreaded Apps a Breeze with ACE! • Wrap up projects sooner –– plug your app-specific code into ACE's C++ frameworks and go • Stress-free porting across a wide array of platforms • Open Source with unlimited commercial use –– no strings attached • Riverace offers world-class expertise, training, and support to help you make the most of ACE's power and flexibility • Come on over and download the latest ACE source kit free www.riverace.com · 888 384 8154 Adding Service to Open Source Software November 2007 l www.ddj.com l Dr. Dobb’s Journal 59 http://gotw.ca/publications/mill36.htm http://www.gotw.ca http://www.riverace.com http://www.riverace.com http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - November 2007 Contents Hmmmm Alia Vox Developer Diaries Developer’s Notebook Smart Compilers - But Smart Enough? Conversations Grid-Enabling Resource-Intensive Applications Distributed Computing: Windows and Linux Adobe AIR: Desktop/Web Convergence Transparency on Demand Reusable Associations Effective Concurrency The Agile Edge Swaine’s Flames Dr. Dobb's Journal - November 2007 Dr. Dobb's Journal - November 2007 - (Page Cover1) Dr. Dobb's Journal - November 2007 - (Page Cover2) Dr. Dobb's Journal - November 2007 - (Page 1) Dr. Dobb's Journal - November 2007 - (Page 2) Dr. Dobb's Journal - November 2007 - (Page 3) Dr. Dobb's Journal - November 2007 - Contents (Page 4) Dr. Dobb's Journal - November 2007 - Contents (Page 5) Dr. Dobb's Journal - November 2007 - Hmmmm (Page 6) Dr. Dobb's Journal - November 2007 - Hmmmm (Page 7) Dr. Dobb's Journal - November 2007 - Hmmmm (Page 8) Dr. Dobb's Journal - November 2007 - Hmmmm (Page 9) Dr. Dobb's Journal - November 2007 - Alia Vox (Page 10) Dr. Dobb's Journal - November 2007 - Alia Vox (Page 11) Dr. Dobb's Journal - November 2007 - Developer Diaries (Page 12) Dr. Dobb's Journal - November 2007 - Developer Diaries (Page 13) Dr. Dobb's Journal - November 2007 - Developer’s Notebook (Page 14) Dr. Dobb's Journal - November 2007 - Developer’s Notebook (Page 15) Dr. Dobb's Journal - November 2007 - Smart Compilers - But Smart Enough? (Page 16) Dr. Dobb's Journal - November 2007 - Smart Compilers - But Smart Enough? (Page 17) Dr. Dobb's Journal - November 2007 - Smart Compilers - But Smart Enough? (Page 18) Dr. Dobb's Journal - November 2007 - Smart Compilers - But Smart Enough? (Page 19) Dr. Dobb's Journal - November 2007 - Conversations (Page 20) Dr. Dobb's Journal - November 2007 - Conversations (Page 21) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 22) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 23) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 24) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 25) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 26) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 27) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 28) Dr. Dobb's Journal - November 2007 - Grid-Enabling Resource-Intensive Applications (Page 29) Dr. Dobb's Journal - November 2007 - Distributed Computing: Windows and Linux (Page 30) Dr. Dobb's Journal - November 2007 - Distributed Computing: Windows and Linux (Page 31) Dr. Dobb's Journal - November 2007 - Distributed Computing: Windows and Linux (Page 32) Dr. Dobb's Journal - November 2007 - Distributed Computing: Windows and Linux (Page 33) Dr. Dobb's Journal - November 2007 - Distributed Computing: Windows and Linux (Page 34) Dr. Dobb's Journal - November 2007 - Distributed Computing: Windows and Linux (Page 35) Dr. Dobb's Journal - November 2007 - Adobe AIR: Desktop/Web Convergence (Page 36) Dr. Dobb's Journal - November 2007 - Adobe AIR: Desktop/Web Convergence (Page 37) Dr. Dobb's Journal - November 2007 - Adobe AIR: Desktop/Web Convergence (Page 38) Dr. Dobb's Journal - November 2007 - Adobe AIR: Desktop/Web Convergence (Page 39) Dr. Dobb's Journal - November 2007 - Adobe AIR: Desktop/Web Convergence (Page 40) Dr. Dobb's Journal - November 2007 - Adobe AIR: Desktop/Web Convergence (Page 41) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 42) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 43) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 44) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 45) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 46) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 47) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 48) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 49) Dr. Dobb's Journal - November 2007 - Transparency on Demand (Page 50) Dr. Dobb's Journal - November 2007 - Reusable Associations (Page 51) Dr. Dobb's Journal - November 2007 - Reusable Associations (Page 52) Dr. Dobb's Journal - November 2007 - Reusable Associations (Page 53) Dr. Dobb's Journal - November 2007 - Reusable Associations (Page 54) Dr. Dobb's Journal - November 2007 - Reusable Associations (Page 55) Dr. Dobb's Journal - November 2007 - Reusable Associations (Page 56) Dr. Dobb's Journal - November 2007 - Effective Concurrency (Page 57) Dr. Dobb's Journal - November 2007 - Effective Concurrency (Page 58) Dr. Dobb's Journal - November 2007 - Effective Concurrency (Page 59) Dr. Dobb's Journal - November 2007 - The Agile Edge (Page 60) Dr. Dobb's Journal - November 2007 - The Agile Edge (Page 61) Dr. Dobb's Journal - November 2007 - The Agile Edge (Page 62) Dr. Dobb's Journal - November 2007 - The Agile Edge (Page 63) Dr. Dobb's Journal - November 2007 - Swaine’s Flames (Page 64) Dr. Dobb's Journal - November 2007 - Swaine’s Flames (Page Cover3) Dr. Dobb's Journal - November 2007 - Swaine’s Flames (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.