Dr. Dobb's Journal - February 2009 - (Page 43) d02sutt_p2db 12/12/08 9:12 AM Page 43 v = 1; local = v; // C: write to v // D: read from v to a = 1; // C: write to v local = 1; // D’: invalid, cannot do // “constant propagation” to eliminate the read from v. For example, v could be a hardware address that automatically increments itself every time it’s written, so that writing 1 will yield the value 2 on the next read. Second, what about nearby ordinary reads and writes—can those still be reordered around unoptimizable reads and writes? Today, there is no practical portable answer because C/C++ compiler implementations vary widely and aren’t likely to converge anytime soon. For example, one interpretation of the C++ Standard holds that ordinary reads can move freely in either direction across a C/C++ volatile read or write, but that an ordinary write cannot move at all across a C/C++ volatile read or write—which would make C/C++ volatile both less restrictive and more restrictive, respectively, than an ordered atomic. Some compiler vendors support that interpretation; others don’t optimize across volatile reads or writes at all; and still others have their own preferred semantics. [2] See www.boost.org. [3] H. Sutter. “Apply Critical Sections Consistently” (DDJ, November 2007). Available online at www.ddj.com/hpc-highperformance-computing/202401098. [4] A common objection at this point is: “In the original code, it was possible for another thread to see the intermediate value, but that is impossible in the transformed code. Isn’t that a change in observable behavior?” The answer is, “No,” because the program was never guaranteed to ever actually interleave just in time to see that value; it was already a legal outcome for this thread to just always run so fast that the interleaving happened to never manifest. Again, what this optimization does is reduce the set of possible executions, which is always legal. [5] S. Liang. Java Native Interface: Programmer’s Guide and Specification. (Prentice Hall, 1999). Available online at java .sun.com/docs/books/jni/. DDJ Herb is a bestselling author and consultant on software development topics, and a software architect at Microsoft. He can be contacted at www.gotw.ca. PC -lint 9.0 for C/C++ Presents Bug of the Month #587 Summary To safely write lock-free code that communicates between threads without using locks, prefer to use ordered atomic variables: Java/.NET volatile, C++0x atomic , and C-compatible atomic_T. To safely communicate with special hardware or other memory that has unusual semantics, use unoptimizable variables: ISO C/C++ volatile. Remember that reads and writes of these variables are not necessarily atomic, however. Finally, to express a variable that both has unusual semantics and has any or all of the atomicity and/or ordering guarantees needed for lock-free coding, only the ISO C++0x draft Standard provides a direct way to spell it: volatile atomic . Much to the chagrin of European bankers, Abraham Lincoln issued greenbacks to finance the Civil War. This program represents an early effort to detect counterfeit greenbacks. But something’s going wrong. Can you spot it? Visit our web site at www.gimpel.com #include // check_serial returns true if the // serial number is OK. bool check_serial( unsigned serial ) { if( (serial & 0x2E537B4) == 0x2C42540 ) return true; else return false; } // main() returns 0 if the serial number // is OK and non-zero otherwise. int main( int argc, char* argv[] ) { unsigned serial_no; if( argc <= 1 ) return 2; serial_no = (unsigned) atoi( argv[1] ); if( check_serial( serial_no ) ) return 0; else return 1; } PC-lint for C/C++ will catch this and many other bugs. It will analyze a mixed suite of C and C++ modules to uncover bugs, glitches, quirks and inconsistencies. Introducing Version 9.0: What, you ask, can be added to our ground-breaking lint products – products that already contain inter-function value tracking, auto variable and member value tracking, customizable function semantics, a comprehensive system of message suppression, and a rich set of over 500 Warnings that all together provide an environment in which few bugs can survive and none can flourish? The answer to that question is found in Version 9.0, which provides support for precompiled headers, static variable tracking, muti-thread analysis, support for MISRA C 2004 and MISRA C++ 2008, a rich set of program information reports including stack usage, and over 146 new messages. Full Support for ANSI/ISO C and C++ WN N NE SIO R VE $389 Call PC-lint for C/C++ FlexeLint for C/C++ Numerous compilers/libraries supported. Runs on Windows, MS-DOS, and OS/2. The same great product for all UNIX systems Distributed in shrouded C source form. 30 Day Money Back Guarantee Notes [1] H. Sutter. “Writing Lock-Free Code: A Corrected Queue” (DDJ, October 2008). Available online at www.ddj.com/hpc-highperformance-computing/210604448. G i m p e l S o f t wa r e Serving the C/C++ Community for 22 Years CALL TODAY (610) 584-4261 or FAX (610) 584-4266 www.gimpel.com PC-lint and FlexeLint are trademarks of Gimpel Software February 2009 l www.ddj.com l Dr. Dobb’s Journal 43 http://www.boost.org http://www.ddj.com/hpc-high-performance-computing/202401098 http://www.ddj.com/hpc-high-performance-computing/202401098 http://javasun.com/docs/books/jni/ http://javasun.com/docs/books/jni/ http://www.gotw.ca http://www.gimpel.com http://www.gimpel.com http://www.ddj.com/hpc-high-performance-computing/210604448 http://www.gimpel.com http://www.ddj.com/hpc-high-performance-computing/210604448 http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - February 2009 Dr. Dobb's Journal - February 2009 Contents Friday Night Fish Fry Alia Vox Developer Diaries Conversations Computing in the Clouds Software Development in the Cloud Videos and Oracle Forms 10g Parallel LINQ Decoupling C Header Files Effective Concurrency Disciplined Agility Swaine’s Flames Dr. Dobb's Journal - February 2009 Dr. Dobb's Journal - February 2009 - (Page BB1) Dr. Dobb's Journal - February 2009 - (Page BB2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page Cover1) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page Cover2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 1) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 2) Dr. Dobb's Journal - February 2009 - Dr. Dobb's Journal - February 2009 (Page 3) Dr. Dobb's Journal - February 2009 - Contents (Page 4) Dr. Dobb's Journal - February 2009 - Contents (Page 5) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 6) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 7) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 8) Dr. Dobb's Journal - February 2009 - Friday Night Fish Fry (Page 9) Dr. Dobb's Journal - February 2009 - Alia Vox (Page 10) Dr. Dobb's Journal - February 2009 - Alia Vox (Page 11) Dr. Dobb's Journal - February 2009 - Developer Diaries (Page 12) Dr. Dobb's Journal - February 2009 - Developer Diaries (Page 13) Dr. Dobb's Journal - February 2009 - Conversations (Page 14) Dr. Dobb's Journal - February 2009 - Conversations (Page 15) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 16) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 17) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 18) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 19) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 20) Dr. Dobb's Journal - February 2009 - Computing in the Clouds (Page 21) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 22) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 23) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 24) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 25) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 26) Dr. Dobb's Journal - February 2009 - Software Development in the Cloud (Page 27) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 28) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 29) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 30) Dr. Dobb's Journal - February 2009 - Videos and Oracle Forms 10g (Page 31) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 32) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 33) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 34) Dr. Dobb's Journal - February 2009 - Parallel LINQ (Page 35) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 36) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 37) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 38) Dr. Dobb's Journal - February 2009 - Decoupling C Header Files (Page 39) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 40) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 41) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 42) Dr. Dobb's Journal - February 2009 - Effective Concurrency (Page 43) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 44) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 45) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 46) Dr. Dobb's Journal - February 2009 - Disciplined Agility (Page 47) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page 48) Dr. Dobb's Journal - February 2009 - Swaine’s Flames (Page Cover3) Dr. Dobb's Journal - February 2009 - 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.