Dr. Dobb's Journal - July 2008 - (Page 46) Core Technology LOCK-FREE QUEUES queue and write to the other. The result is a controlled infinite loop. By limiting the game to a fixed number of reads/writes (“shots”), you get an understanding of how the queue behaves when varying the waiting/sleep time and strategy and the number of “balls.” The faster the game, the better the performance. You should also check CPU usage to see how much of it is used for real work. • “No ball” means “do nothing” (like two players waiting for the other to start). This gives you an idea of how good the queues are when there is no data—how nervous the players are. Ideally, CPU usage should be zero. • “One ball” is like the real ping-pong game: Each player shoots and waits for the other to shoot. • “Two (or more) balls” means both players could shoot at the same time, modulo collision and waiting issues. In a wait-free system, the more balls in the game, the better the performance gain compared to the classic locking strategy. This is because wait-free is an optimistic concurrency control method (works best when there is no contention), while classical lock-based concurrency control is pessimistic (assumes contention happens and preemptively inserts locking). Ready to play? Here is the Ping-Pong test command line: $> ./pingpong [strategy] [timeout] [balls] [shots] Figure 4: Classic approach results. When you run the program, the tests show the results in the table shown in Figure 3: • The best combination is the timed_wait() with a small wait time (1ms in the test for TIMED_WAIT). It has a very fast response time and almost 0 percent CPU usage when the queue is empty. • Even when the sleep time is 0 (usleep(0)), the worst seems to be the sleep() method, especially when the queue is likely to be empty. (The number of shots in this case is 100-times smaller than the other cases because of the long duration of the game.) • The NO_WAIT strategy is fast but behaves worst when there are no balls (100-percent CPU usage to do nothing). It has the best performance when there is a single ball. Figure 4 presents a table with the results for a classic approach (see SafeQueue). These results show that this queue is, on average, more than four-times slower than the LockFreeQueue. The slowdown comes from the synchronization between threads. Both Produce() and Consume() have to wait for each other to finish. CPU usage is almost 100 percent for this test (similar to the NO_WAIT strategy, but not even close to its performance). 46 Dr. Dobb’s Journal l www.ddj.com l July 2008 http://www.codejock.com http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - July 2008 Dr. Dobb's Journal - July 2008 Contents Friday Night Fish Fry Alia Vox Developer Diaries Developer’s Notebook Engineers Without Borders Conversations Patricia Tries Event-Based Architectures Graphs Versus Objects Lock-Free Queues Dr. Dobb’s Architecture & Design World Java and the Nokia N10 Internet Tablet Effective Concurrency The Agile Edge Swaine’s Flames Dr. Dobb's Journal - July 2008 Dr. Dobb's Journal - July 2008 - (Page Belly1) Dr. Dobb's Journal - July 2008 - (Page Belly2) Dr. Dobb's Journal - July 2008 - Dr. Dobb's Journal - July 2008 (Page Cover1) Dr. Dobb's Journal - July 2008 - Dr. Dobb's Journal - July 2008 (Page Cover2) Dr. Dobb's Journal - July 2008 - Dr. Dobb's Journal - July 2008 (Page 1) Dr. Dobb's Journal - July 2008 - Contents (Page 2) Dr. Dobb's Journal - July 2008 - Contents (Page 3) Dr. Dobb's Journal - July 2008 - Friday Night Fish Fry (Page 4) Dr. Dobb's Journal - July 2008 - Friday Night Fish Fry (Page 5) Dr. Dobb's Journal - July 2008 - Friday Night Fish Fry (Page 6) Dr. Dobb's Journal - July 2008 - Friday Night Fish Fry (Page 7) Dr. Dobb's Journal - July 2008 - Alia Vox (Page 8) Dr. Dobb's Journal - July 2008 - Alia Vox (Page 9) Dr. Dobb's Journal - July 2008 - Developer Diaries (Page 10) Dr. Dobb's Journal - July 2008 - Developer Diaries (Page 11) Dr. Dobb's Journal - July 2008 - Developer’s Notebook (Page 12) Dr. Dobb's Journal - July 2008 - Developer’s Notebook (Page 13) Dr. Dobb's Journal - July 2008 - Engineers Without Borders (Page 14) Dr. Dobb's Journal - July 2008 - Engineers Without Borders (Page 15) Dr. Dobb's Journal - July 2008 - Engineers Without Borders (Page 16) Dr. Dobb's Journal - July 2008 - Engineers Without Borders (Page 17) Dr. Dobb's Journal - July 2008 - Conversations (Page 18) Dr. Dobb's Journal - July 2008 - Conversations (Page 19) Dr. Dobb's Journal - July 2008 - Patricia Tries (Page 20) Dr. Dobb's Journal - July 2008 - Patricia Tries (Page 21) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 22) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 23) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 24) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 25) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 26) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 27) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 28) Dr. Dobb's Journal - July 2008 - Event-Based Architectures (Page 29) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 30) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 31) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 32) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 33) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 34) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 35) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 36) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 37) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 38) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 39) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 40) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 41) Dr. Dobb's Journal - July 2008 - Graphs Versus Objects (Page 42) Dr. Dobb's Journal - July 2008 - Lock-Free Queues (Page 43) Dr. Dobb's Journal - July 2008 - Lock-Free Queues (Page 44) Dr. Dobb's Journal - July 2008 - Lock-Free Queues (Page 45) Dr. Dobb's Journal - July 2008 - Lock-Free Queues (Page 46) Dr. Dobb's Journal - July 2008 - Lock-Free Queues (Page 47) Dr. Dobb's Journal - July 2008 - Dr. Dobb’s Architecture & Design World (Page 48) Dr. Dobb's Journal - July 2008 - Dr. Dobb’s Architecture & Design World (Page 49) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 50) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 51) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 52) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 53) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 54) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 55) Dr. Dobb's Journal - July 2008 - Java and the Nokia N10 Internet Tablet (Page 56) Dr. Dobb's Journal - July 2008 - Effective Concurrency (Page 57) Dr. Dobb's Journal - July 2008 - Effective Concurrency (Page 58) Dr. Dobb's Journal - July 2008 - Effective Concurrency (Page 59) Dr. Dobb's Journal - July 2008 - The Agile Edge (Page 60) Dr. Dobb's Journal - July 2008 - The Agile Edge (Page 61) Dr. Dobb's Journal - July 2008 - The Agile Edge (Page 62) Dr. Dobb's Journal - July 2008 - The Agile Edge (Page 63) Dr. Dobb's Journal - July 2008 - Swaine’s Flames (Page 64) Dr. Dobb's Journal - July 2008 - Swaine’s Flames (Page Cover3) Dr. Dobb's Journal - July 2008 - 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.