<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>CHESS: Find and Reproduce Concurrency Heisenbugs</title><link>http://blogs.msdn.com/chess/default.aspx</link><description>This blog contains articles about CHESS, a tool from Microsoft Research. For more details visit http://research.microsoft.com/chess</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>CHESS release v0.1.30610.2: Data Race Detection, ChessBoard, Refinement Checking</title><link>http://blogs.msdn.com/chess/archive/2009/06/12/chess-release-v0-1-30610-2-data-race-detection-chessboard-refinement-checking.aspx</link><pubDate>Fri, 12 Jun 2009 21:15:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9737000</guid><dc:creator>CHESS Tool</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/chess/comments/9737000.aspx</comments><wfw:commentRss>http://blogs.msdn.com/chess/commentrss.aspx?PostID=9737000</wfw:commentRss><description>&lt;P&gt;The CHESS team is happy to announce a &lt;A href="http://msdn.microsoft.com/en-us/devlabs/cc950526.aspx" mce_href="http://msdn.microsoft.com/en-us/devlabs/cc950526.aspx"&gt;new release of CHESS&lt;/A&gt;, which includes a whole bunch of new features for concurrency testing of managed code and fixes for bugs reported on the &lt;A href="http://social.msdn.microsoft.com/Forums/en-us/chess/" mce_href="http://social.msdn.microsoft.com/Forums/en-us/chess/"&gt;CHESS MSDN forum&lt;/A&gt;&amp;nbsp;(&lt;A href="http://channel9.msdn.com/posts/Peli/Data-Race-Detection-with-CHESS/" mce_href="http://channel9.msdn.com/posts/Peli/Data-Race-Detection-with-CHESS/"&gt;see the Channel 9 movie&lt;/A&gt;). Major features in this release include:&lt;/P&gt;
&lt;P&gt;1. &lt;B&gt;Data race detection&lt;/B&gt; for managed code;&lt;/P&gt;
&lt;P&gt;2. &lt;B&gt;ChessBoard&lt;/B&gt;, an interactive shell for CHESS that simplifies the typical user interactions with CHESS, such as launching CHESS runs and managing test results (it is especially useful for finding and reproducing data races);&lt;/P&gt;
&lt;P&gt;3. &lt;B&gt;Refinement checking&lt;/B&gt; for comparing the observed concurrent behaviors of a concurrent data type with its observable serial behaviors.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Data Race Detection&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;By default, CHESS places preemptions in your code at before every synchronization call (like Monitor.Enter), before every volatile access (read or write), and before every interlocked operation. If you have two threads accessing the same (non-volatile) memory location without synchronization or an interlocked operation, your program probably has data races, which can lead to unpredictable behavior. With data race detection (/detectraces), you now can find this important class of errors with CHESS. The screen snapshot below shows a data race found with CHESS in Stephen Toub’s ThreadPoolWait example from MSDN.&lt;/P&gt;
&lt;P&gt;&lt;IMG style="WIDTH: 671px; HEIGHT: 339px" title="Data Race" alt="Data Race" src="http://research.microsoft.com/en-us/projects/chess/datarace2.jpg" width=671 height=339 mce_src="http://research.microsoft.com/en-us/projects/chess/datarace2.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;ChessBoard&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;The main goals of ChessBoard are to simplify simultaneous running of multiple tests and viewing of multiple results, quick repro &amp;amp; display of failing testcases and races, simplify experimentation with various CHESS parameters. The screen snapshot below shows the organization of ChessBoard into four panes: Test View, Task View, Result View, and Detail View. &lt;/P&gt;
&lt;P&gt;&lt;IMG style="WIDTH: 700px; HEIGHT: 468px" title=ChessBoard alt=ChessBoard src="http://research.microsoft.com/en-us/projects/chess/chessboard.jpg" width=700 height=468 mce_src="http://research.microsoft.com/en-us/projects/chess/chessboard.jpg"&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Refinement Checking&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;When creating large suites of small concurrent tests, it can be cumbersome to write tight assertions that validate the behavior of the tested code. A new CHESS feature, refinement checking, simplifies this task for the case of concurrent data types, by comparing the observed concurrent behaviors with the observable serial behaviors. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Controlling Execution Time of CHESS&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/maxexecs:N&lt;/B&gt;, the maximum number of thread schedules CHESS should explore (default: unlimited) &lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/maxexectime:N&lt;/B&gt;, the maximum number of seconds an execution of ChessTest.Run is allowed to take (default: 10 seconds)&lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/maxchesstime:N&lt;/B&gt;, the maximum number of seconds the entire CHESS explorartion is allowed to take (default: unlimited)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Other New Options&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/diagnose, &lt;/B&gt;prints (at the end of the CHESS run) which types were instrumented and which were not.&lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/showprogress,&lt;/B&gt; to make CHESS periodically print an estimate of how many schedules remain to be explored&lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/processorcount:N&lt;/B&gt;, makes Environment.ProcessorCount return the value N (N&amp;gt;=1). By default, mchess returns 1 whenever Environment.ProcessorCount is called. Very useful for testing code that spins based on processor count.&lt;/P&gt;
&lt;P&gt;- &lt;B&gt;/outputprefix&lt;/B&gt;, prefix to prepend before each file CHESS reads/writes (sched, results.xml, etc). Very useful if you are doing multiple CHESS runs in the same directory.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Other Features and Fixes:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;- New samples:&lt;/P&gt;
&lt;UL&gt;
&lt;UL&gt;
&lt;LI&gt;ThreadPoolWait Sample (data race)&lt;/LI&gt;
&lt;LI&gt;SimpleBank Sample (data race)&lt;/LI&gt;&lt;/UL&gt;&lt;/UL&gt;
&lt;P&gt;- /volatile is on by default (use /volatile- to turn off)&lt;/P&gt;
&lt;P&gt;- mchess now handles&lt;/P&gt;
&lt;UL&gt;
&lt;UL&gt;
&lt;LI&gt;BeginInvoke/EndInvoke&lt;/LI&gt;
&lt;LI&gt;Join with timeout&lt;/LI&gt;
&lt;LI&gt;WaitOne with timeout&lt;/LI&gt;&lt;/UL&gt;&lt;/UL&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9737000" width="1" height="1"&gt;</description></item><item><title>Getting the most out of CHESS</title><link>http://blogs.msdn.com/chess/archive/2009/02/17/getting-the-most-out-of-chess.aspx</link><pubDate>Tue, 17 Feb 2009 09:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9427190</guid><dc:creator>CHESS Tool</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/chess/comments/9427190.aspx</comments><wfw:commentRss>http://blogs.msdn.com/chess/commentrss.aspx?PostID=9427190</wfw:commentRss><description>&lt;P&gt;Hi, this is Shaz Qadeer from the CHESS team.&amp;nbsp; If you have visited our blog before, you have probably read our articles about systematic concurrency testing using CHESS, design of the CHESS scheduler, and the advantages of CHESS testing over stress testing.&amp;nbsp; Hopefully, these articles have convinced you that there is substantial value in using CHESS to test your concurrent programs.&amp;nbsp; In this article, I will try to explain the best practices in using CHESS.&amp;nbsp; In other words, I will attempt to answer the question:&amp;nbsp; How do I get the most out of CHESS?&lt;/P&gt;
&lt;P&gt;Before I answer that question, I would like to review the anatomy of a test suitable for being used with CHESS.&amp;nbsp; A CHESS test comprises three functions: ChessTestStartup, ChessTestRun, and ChessTestShutdown.&amp;nbsp;&amp;nbsp;Given these functions, CHESS performs the following computation:&lt;/P&gt;
&lt;H5&gt;ChessTestStartup();&lt;/H5&gt;
&lt;H5&gt;while (exists unexplored schedule) {&lt;/H5&gt;
&lt;H5&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ChessTestRun();&lt;/H5&gt;
&lt;H5&gt;}&lt;/H5&gt;
&lt;H5&gt;ChessTestShutdown();&lt;/H5&gt;
&lt;P&gt;After executing ChessTestStartup once, ChessTestRun is executed repeatedly, each execution taking a different schedule of the multithreaded scenario in ChessTestRun.&amp;nbsp; If an execution results in a crash, manifested either as an assertion failure or an uncaught exception, then CHESS terminates.&amp;nbsp; At this point, CHESS provides you with the capability to&amp;nbsp;execute ChessTestRun along the thread schedule that caused the crash.&amp;nbsp;If no error is found,&amp;nbsp;the test finishes after executing ChessTestShutdown.&lt;/P&gt;
&lt;P&gt;The most important aspect of using CHESS is to identify the concurrency scenario to be tested.&amp;nbsp; For example, suppose we want to test a concurrent non-blocking queue data structure that supports two operations Enqueue and Dequeue.&amp;nbsp; There are several interesting concurrency scenarios for this data structure, e.g., two Enqueue operations in parallel, two Dequeue operations in parallel, an Enqueue and a Dequeue in parallel, etc.&amp;nbsp; Each of these scenarios should be given to CHESS as a separate test.&amp;nbsp; Remember that CHESS is systematically searching the huge set of executions of a concurrent program and the number of executions increases exponentially with the size of the program.&amp;nbsp; You can help CHESS by factoring out your large test into a collection of smaller scenario tests.&amp;nbsp; &lt;/P&gt;
&lt;P&gt;To test the first scenario of two parallel Enqueue operations, ChessTestStartup and ChessTestShutdown should be empty and ChessTestRun should perform the following sequence of operations:&lt;/P&gt;
&lt;H5&gt;1.&amp;nbsp; Allocate and initialize the queue.&lt;/H5&gt;
&lt;H5&gt;2.&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;Create two threads each calling the Enqueue operation once, wait for the threads to finish, and reset the queue.&lt;/STRONG&gt;&lt;/H5&gt;
&lt;H5&gt;3.&amp;nbsp;&amp;nbsp;Free the queue.&lt;/H5&gt;
&lt;P&gt;Two aspects of step 2 in ChessTestRun are worth noting.&amp;nbsp; First, it makes each thread perform an operation only once.&amp;nbsp; This is in sharp contrast to the approach of stress testing, where each thread is usually made to repeat the same operation many times.&amp;nbsp; Repetition is built into the CHESS framework (see the pseudo-code above), so your test does not have to do it.&amp;nbsp; Second, ChessTestRun waits for all child threads to finish and resets the queue state.&amp;nbsp; This is important because CHESS expects every execution of ChessTestRun to end in a state that is equivalent to the state at the beginning.&amp;nbsp; You can do this by simply waiting for all threads, releasing all handles and resetting any global state used by the test. If ChessTestRun does not satisfy this property then CHESS might be unable to reproduce a bug discovered during its search.&amp;nbsp; We have observed that most scenarios naturally satisfy this property.&amp;nbsp; But sometimes global or static variables are initialized lazily during the first execution of ChessTestRun, thereby making the states at the beginning and end of the first execution of ChessTestRun different from each other.&amp;nbsp; This problem is easily fixed though; just call ChessTestRun a couple of&amp;nbsp; times at the end of ChessTestStartup.&amp;nbsp; This is often sufficient to trigger all lazy initialization in the test. With this trick, subsequent calls to ChessTestRun have the same begin and end states.&lt;/P&gt;
&lt;P&gt;For testing the non-blocking queue, we didn’t really use ChessTestStartup and ChessTestShutdown.&amp;nbsp; These functions tend to be used relatively infrequently for the expensive part of test setup and teardown.&amp;nbsp; For example, if the test connects to a database server, then the connection to the server&amp;nbsp;could be&amp;nbsp;established in TestStartup and torn down in TestShutdown.&lt;/P&gt;
&lt;P&gt;Now, imagine that CHESS successfully generates all executions of the test with two threads each calling the Enqueue operation without finding an error.&amp;nbsp; What does that say about the executions of a test with three such threads?&amp;nbsp; Theoretically, the test with three threads may uncover a bug that does not occur with two threads.&amp;nbsp; At the same time, the number of possible executions grows exponentially with the number of threads; consequently, testing with large number of threads will consume exponentially more testing resources.&amp;nbsp; So what should our testing strategy be?&amp;nbsp; &lt;/P&gt;
&lt;P&gt;The systematic testing approach of CHESS is predicated upon the small-scope hypothesis: the likelihood of finding new errors in a parameterized system goes down as the parameter values are increased.&amp;nbsp; In other words, systematic exploration of executions for small parameter values is highly effective in flushing out many bugs.&amp;nbsp; A large amount of empirical evidence supporting this hypothesis has been obtained by us and by others working in the broad area of formal analysis of computer systems. An implication of the small-scope hypothesis is that an iterative strategy works best.&amp;nbsp; Start with a small number of threads and increase the number gradually depending on your testing resources.&amp;nbsp; This strategy also has the advantage that you get partial yet quantifiable coverage as exploration for a given number of threads is finished.&lt;/P&gt;
&lt;P&gt;Another way to look at this testing strategy is as follows. If a bug requires a minimum of three threads, run CHESS with exactly three threads. Unlike stress, CHESS is guaranteed to find this bug. By iteratively increasing the number of threads, you are increasing the complexity of the bugs you find and the coverage achieved. Another advantage of this iterative strategy is that for every bug found, you find the simplest scenario that triggers the bug. This tremendously improves your debugging experience. &lt;/P&gt;
&lt;P&gt;Finally, I would like to mention some common difficulties our users have encountered in creating a test suitable to be used with CHESS.&amp;nbsp; For each issue, we also describe a possible workaround.&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp; A background thread starts during ChessTestStartup and finishes during ChessTestShutdown.&amp;nbsp; This thread executes outside the control of CHESS, since CHESS controls the scheduling of only the threads created during the execution of ChessTestRun.&amp;nbsp; If this thread modifies the state of the test, then CHESS’ mechanism for reproducing executions may get confused.&amp;nbsp; The simplest workaround is to make ChessTestStartup and ChessTestShutdown be a part of ChessTestRun, thereby bringing the background thread under the control of CHESS.&amp;nbsp; Of course, you will now pay the performance penalty of having the startup and shutdown code exceuted during each iteration of ChessTestRun.&lt;/P&gt;
&lt;P&gt;2.&amp;nbsp; ChessTestRun uses a custom threadpool, perhaps implemented using&amp;nbsp;System.Threading or WIN32&amp;nbsp;threads, to schedule tasks asynchronously.&amp;nbsp; In such threadpool implementations, threads are often allocated dynamically and the number of threads may not be the same at the beginning and end of ChessTestRun.&amp;nbsp; In this case, the fix is to implement a shutdown&amp;nbsp;function&amp;nbsp;that tears down all internal threads in the threadpool and ensure&amp;nbsp;that this function&amp;nbsp;is called at the end of ChessTestRun.&lt;/P&gt;
&lt;P&gt;Well, that is all I have to say for now.&amp;nbsp; There is a lot of information here on how to use CHESS effectively.&amp;nbsp; I hope it helps all you CHESS enthusiasts out there.&amp;nbsp; Please do let me know if you have any questions.&lt;/P&gt;
&lt;H5&gt;Best regards,&lt;/H5&gt;
&lt;H5&gt;Shaz&lt;/H5&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9427190" width="1" height="1"&gt;</description></item><item><title>Stress vs. CHESS</title><link>http://blogs.msdn.com/chess/archive/2009/02/02/stress-vs-chess.aspx</link><pubDate>Tue, 03 Feb 2009 02:29:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9391977</guid><dc:creator>CHESS Tool</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/chess/comments/9391977.aspx</comments><wfw:commentRss>http://blogs.msdn.com/chess/commentrss.aspx?PostID=9391977</wfw:commentRss><description>&lt;p&gt;Hey there... it's time for another post. My name is Sebastian Burckhardt, I am a member of the CHESS team, and I want to tell you about some experiments I ran today. The question that interests me here is &lt;em&gt;&lt;strong&gt;how does CHESS compare to stress testing ?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;First, let me clarify the difference between the two. In general, the purpose of stress testing is to find all sorts of deficiencies under heavy load, including performance problems, excessive memory consumption, or insufficient reliability under failures. For example, we may create stress by using very large files or databases, or by processing a very large number of requests. &lt;/p&gt;  &lt;p&gt;In contrast, CHESS specifically targets concurrency errors such as race conditions, synchronization errors or deadlocks. Even though such errors have nothing to do with stress per se, programmer often use stress testing to find them. Why is that so? I believe the answer is that stress can create unusual interleavings. When under heavy load, the relative speed of threads changes because some operations are slowed down while others are not. This helps to uncover synchronization errors. Thus, we can consider &lt;em&gt;&lt;strong&gt;improved interleaving coverage&lt;/strong&gt;&lt;/em&gt; a (secondary) effect of stress testing. Clearly, it's not the original purpose of stress. Nor is arbitrary creation of &amp;quot;stress&amp;quot; very efficient, as we will see later.&lt;/p&gt;  &lt;p&gt;CHESS on the other hand is all about systematic coverage of interleavings. It's its primary purpose! So, while it will definitely not find problems related to files being too large, it does a terrific job at finding concurrency errors.&lt;/p&gt;  &lt;p&gt;We're now going to try to find a bug with both stress testing and with CHESS (here is where the fun starts). First, go ahead and download &amp;amp; install CHESS (or just keep reading). Then, open the Account example (use VS to open Samples/Managed/BankAccount/Bank.sln). It contains the buggy &amp;quot;Account&amp;quot; class:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="image" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_thumb.png" width="215" border="0" /&gt;&lt;/a&gt;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_10.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="543" alt="image" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_thumb_4.png" width="458" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The bug is in the Withdraw() method: the Read() should happen inside the critical section. Here is the unit test that is designed to this bug:&lt;/p&gt;  &lt;pre class="code"&gt;    [&lt;span style="color: #2b91af"&gt;TestClass&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TestBank
    &lt;/span&gt;{
        [&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
        &lt;span style="color: blue"&gt;public void &lt;/span&gt;WithdrawAndDepositConcurrently()
        {
            &lt;span style="color: blue"&gt;var &lt;/span&gt;account = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Account&lt;/span&gt;(10);
            &lt;span style="color: blue"&gt;var &lt;/span&gt;child = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;(
               o =&amp;gt; { (o &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Account&lt;/span&gt;).Withdraw(2); }
               );
            child.Start(account);
            account.Deposit(1);
            child.Join();
            
            &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(9, account.Read());
        }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;When running this test, a child thread is created. The original thread and the child thread then both make method calls to the same account object (calling Deposit(1) and Withdraw(2), respectively). As the starting balance was 10, the ending balance should be 9 if all goes well. However, because of the bug, there is a chance that we end up with a balance of 8 (because of the following scenario: the child thread calls Withdraw(2), reads a balance of 10, then gets preempted; the original thread now calls Deposit(1), updating the balance to 9. Finally, the child thread resumes, writing a balance of 8).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are our chances of hitting the bug with this test?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you may have guessed, they are not very good. You can try running this test a couple of times... it never failed for me. So, next step: stress. We'll start by simply repeating the test, first 1 thousand times, then 10 thousand times, then 100 thousand times. Note that we're using Release build type here (you may want to use smaller numbers for Debug build type).&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Repeat1k()
{
    &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; 1000; ++i)
        WithdrawAndDepositConcurrently();
}&lt;/pre&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Repeat10k()
{
    &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; 10000; ++i)
        WithdrawAndDepositConcurrently();
}&lt;/pre&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;Repeat100k()
{
    &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; 100000; ++i)
        WithdrawAndDepositConcurrently();
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We also add a CHESS test. Note that we need no loop here. CHESS has stress &amp;quot;built-in&amp;quot; because it already tries to systematically cover all interleavings, implicitly repeating the test many times. In fact, a loop would be a bad idea, since it would overwhelm CHESS.&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
[&lt;span style="color: #2b91af"&gt;HostType&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Chess&amp;quot;&lt;/span&gt;)]
&lt;span style="color: blue"&gt;public void &lt;/span&gt;RunOnceWithChess()
{
    WithdrawAndDepositConcurrently();  &lt;span style="color: green"&gt;// no need to write a loop
&lt;/span&gt;}&lt;/pre&gt;

&lt;p&gt;Now, let's run this (choose Tests-&amp;gt;Run-&amp;gt;All Tests in Solution from the menu) and look at the results, sorted by test duration.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_13.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="225" alt="image" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_thumb_3.png" width="729" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, the original test (WithdrawAndDepositConcurrently) and the shortest stress test (Repeat1k) did not find the bug, but CHESS (RunOnceWithChess) and the two larger stress tests (Repeat10k, Repeat100k) both did.&lt;/p&gt;

&lt;p&gt;Note that CHESS was a little faster than stress.&lt;strong&gt; Still, stress testing seems to work. So why do we need CHESS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The answer is: because even under stress, the scheduler tends to heavily favor some interleavings over others. Specifically, there is some basic notion of fairness among threads, which makes it unlikely that a thread that is ready to execute is held back for a very, very long time.&lt;/p&gt;

&lt;p&gt;We can demonstrate this by making a small change to the Account class, by adding a call to WasteTimeOnMeaninglessComputation() to the Deposit() method.&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;Deposit(&lt;span style="color: blue"&gt;int &lt;/span&gt;amount)
{
    WasteTimeOnMeaninglessComputation();
    &lt;span style="color: blue"&gt;lock &lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;)
    {
        balance = balance + amount;
    }
}

&lt;span style="color: blue"&gt;public void &lt;/span&gt;WasteTimeOnMeaninglessComputation()
{
    &lt;span style="color: blue"&gt;double &lt;/span&gt;dummy = 0;
    &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; 50000; i++)
    {
        dummy += &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Sin(i);
    }
    System.&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(dummy);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Of course, the Account class is just as broken as it ever was. We have just added some arbitrary delay to the thread calling the Deposit method. We can easily imagine how that may happen in a real program (for example, some heavy initialization may take place there, or the user may instrument the program to output lots of trace information to the console, maybe even in an attempt to debug a Heisenbug).&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The interesting thing is that this little change throws off the stress testing:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_11.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="222" alt="image" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/Stressvs.CHESS_B162/image_thumb_2.png" width="722" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Even with a 100k repetitions (which now take a lot longer), stress testing did not find the bug. Of course, CHESS is completely unimpressed by our change and finds the bug just as quickly as before.&lt;/p&gt;

&lt;p&gt;So, what's the point? Is stress a bad thing? No. Clearly it does work, at least sometimes. &lt;strong&gt;Maybe if we try a couple more times, maybe if we run more tests, maybe if we change some numbers, maybe if we run on a different machine we'll find the bug&lt;/strong&gt; (some teams run stress tests for days). Indeed, we tried the same tests and some small variations on a couple machines here and sometimes the bug was found. &lt;strong&gt;But then again, maybe we will never find it&lt;/strong&gt; (there is no guarantee that running forever will eventually try all schedules, because the operating system scheduler is not a perfectly random thing). It's just not reliable, and definitely not quick. Also, it's not fun to reproduce and fix a bug that manifests only after days of running on some special machine.&lt;/p&gt;

&lt;p&gt;I believe this simple experiment is making a strong case for using CHESS to find concurrency bugs in unit tests.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9391977" width="1" height="1"&gt;</description></item><item><title>The CHESS scheduler – the key to finding concurrency bugs</title><link>http://blogs.msdn.com/chess/archive/2009/01/20/the-chess-scheduler-the-key-to-finding-concurrency-bugs.aspx</link><pubDate>Tue, 20 Jan 2009 12:34:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9344856</guid><dc:creator>CHESS Tool</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/chess/comments/9344856.aspx</comments><wfw:commentRss>http://blogs.msdn.com/chess/commentrss.aspx?PostID=9344856</wfw:commentRss><description>&lt;p align="justify"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/chessicon.jpg"&gt;&lt;font size="2"&gt;&lt;img title="chessicon" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 5px 10px 0px 0px; border-right-width: 0px" height="100" alt="chessicon" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/chessicon_thumb.jpg" width="100" align="left" border="0" /&gt;&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt; Hey everyone, I am &lt;/font&gt;&lt;a href="http://research.microsoft.com/~madanm"&gt;&lt;font size="2"&gt;Madan Musuvathi&lt;/font&gt;&lt;/a&gt;&lt;font size="2"&gt;, the architect of CHESS. In this blog, I will describe the CHESS scheduler, a key component of CHESS. Before I start, if you haven’t seen Tom’s previous post about the CHESS Devlabs release, you should. Download CHESS, play around, and let us know what you think. &lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;When CHESS attaches to your program, it takes complete control over the scheduling of threads in your program. You are no longer at the mercy of the Windows scheduler, which can nondeterministically preempt your threads whenever it wants. Instead, CHESS, and indirectly you, the user, decide how the threads interleave during execution. This allows CHESS to systematically drive your program along possible interleavings, hunting for bugs. Also, once CHESS finds a bug, the repro is as easy as telling the CHESS scheduler to execute the interleaving that produced that bug.&lt;/font&gt;&lt;/p&gt;  &lt;h1&gt;Running Example&lt;/h1&gt;  &lt;p&gt;&lt;font&gt;To explain how the CHESS scheduler controls the interleaving, lets look at a simple program. (This is our favorite example, you will see it again and again.)&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwith_6.jpg"&gt;&lt;img title="depwith" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="492" alt="depwith" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwith_thumb_2.jpg" width="650" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;h1 align="justify"&gt;&lt;/h1&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;This example involves two threads, a deposit thread that adds $100 to the &lt;strong&gt;balance&lt;/strong&gt;, and a withdraw thread that deducts $100 from the &lt;strong&gt;balance&lt;/strong&gt;. If the balance starts out as $100, we expect the final value to be $100. Both threads protect their accesses to the shared variable &lt;strong&gt;balance &lt;/strong&gt;with a critical section. But, the withdraw thread does not hold the critical section lock for its entire computation, resulting in an ‘atomicity’ violation. The assert at the end fires if the deposit thread executes after the withdraw thread leaves the critical section but before it enters the critical section for the second time. This interleaving is shown below:&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithbug_4.jpg"&gt;&lt;img title="depwithbug" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="495" alt="depwithbug" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithbug_thumb_1.jpg" width="647" border="0" /&gt;&lt;/a&gt; &lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;CHESS can find this bug by systematically exploring all interleavings. &lt;/font&gt;&lt;/p&gt;  &lt;h1&gt;Instrumentation&lt;/h1&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;First, CHESS instruments the input program to make calls to the CHESS scheduler. We use various techniques to perform this instrumentation (which will not be described this blog post). This is how the code above will look after the instrumentation. &lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithins_2.jpg"&gt;&lt;img title="depwithins" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="400" alt="depwithins" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithins_thumb.jpg" width="650" border="0" /&gt;&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;By default, CHESS instruments a call to the CHESS scheduler before every synchronization operation. In mchess, you can use the /volatile option to instrument a call before volatile accesses as well. &lt;/font&gt;&lt;/p&gt;  &lt;h1&gt;Inserting Delays&lt;/h1&gt;  &lt;p&gt;&lt;font size="2"&gt;What does the scheduler to do at ChessSchedule() calls? The goal is to insert delays to force different interleavings. To see this, let us look at a “first-cut” solution, where the scheduler inserts random delays, as shown below:&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithrand_2.jpg"&gt;&lt;img title="depwithrand" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="389" alt="depwithrand" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithrand_thumb.jpg" width="630" border="0" /&gt;&lt;/a&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;Now, depending on how the random numbers are seeded, we are likely to get different interleavings of the two threads on each run. Convince yourselves that for particular random sleep sequences, one can emulate the interleaving that results in the bug shown above. But, there are still many problems with this solution. For instance, many (in fact, infinitely many) random sequences will result in the same interleaving, causing a wastage of test resources. Also, starting from the same random seed, you are still not guaranteed to see the same interleaving --- the Windows scheduler still decides how long a thread sleeps and when to run threads. So, repro is still a problem.&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;CHESS improves upon this “first-cut” solution by selectively blocking and enabling threads, in such a way that the Windows scheduler has absolutely no control. The CHESS scheduler creates a semaphore for each thread. &lt;/font&gt;&lt;font size="2"&gt;At each call to ChessSchedule(), the calling thread queries the scheduler to see if the thread should block, and if so, the it blocks on its semaphore. Simultaneously, the scheduler releases the semaphore of the thread that needs to run next. By doing so, the scheduler is able to orchestrate an interleaving of its choice. For instance, the buggy interleaving can be achieved as follows:&lt;img title="depwithfinal" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="658" alt="depwithfinal" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithfinal_thumb.jpg" width="659" border="0" /&gt;&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;Initially, the semaphore for the two threads “withSem” and “depSem” are not available. At the beginning of the program, the scheduler decides that it wants the withdraw thread to run by releasing withSem. Note, the deposit thread will block on depSem while the withdraw thread makes progress. Also, as the deposit thread is blocked, the Windows scheduler has no choice but to schedule the withdraw thread. After the withdraw thread leaves the critical section, it calls into the CHESS scheduler again (at the instrumentation before the next EnterCriticalSection). At this point, the scheduler decides to block this thread and enables the deposit thread by releasing depSem. Now, the withdraw thread blocks on its semaphore while the deposit thread makes progress. Again, as the Windows scheduler sees that the only enabled thread is the deposit thread, it has no choice but to schedule that thread. &lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;font size="2"&gt;By picking different choices at the instrumentation points, the scheduler is able to force different interleavings. Another interleaving example is shown below:&lt;/font&gt;&lt;/p&gt;  &lt;p align="justify"&gt;&lt;a href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithfinal2_2.jpg"&gt;&lt;img title="depwithfinal2" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="597" alt="depwithfinal2" src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/3ad26c418d89_EB85/depwithfinal2_thumb.jpg" width="659" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;Repro is now simple. Before an execution, the scheduler dumps to disk the choices it is going to make at each ChessSchedule() call. For repro, the scheduler simply reads these choices from the disk.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;There are still some intricacies involved: as the scheduler inserts artificial synchronizations with these semaphores, these can conflict with other synchronizations in the program resulting in a deadlock. Imagine, a thread holds a lock and then blocks on the semaphore that CHESS has created for that thread. Now, the CHESS scheduler schedules another thread that simply goes and blocks on the lock, we have a deadlock that CHESS introduced. While we try hard to find your bugs, we don’t want you to be chasing false bugs that we generated. The CHESS scheduler is smart enough and such deadlocks don’t happen. If you are a concurrency wizard, you probably know how hard this can be, especially when programs can use a variety of synchronizations, including spinlocks. But, as we have solved that problem, you can sleep tight and be a blissful user of CHESS :)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="2"&gt;&amp;#160;&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9344856" width="1" height="1"&gt;</description></item><item><title>CHESS Release on MSDN DevLabs</title><link>http://blogs.msdn.com/chess/archive/2009/01/09/chess-release-on-msdn-devlabs.aspx</link><pubDate>Fri, 09 Jan 2009 04:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9301059</guid><dc:creator>CHESS Tool</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/chess/comments/9301059.aspx</comments><wfw:commentRss>http://blogs.msdn.com/chess/commentrss.aspx?PostID=9301059</wfw:commentRss><description>&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image001_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image001_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=100 alt=clip_image001 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image001_thumb.jpg" width=100 align=left border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image001_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Hi. This is &lt;A href="http://research.microsoft.com/~tball/" mce_href="http://research.microsoft.com/~tball/"&gt;Tom Ball&lt;/A&gt;. I am a Principal Researcher at &lt;A href="http://research.microsoft.com/" mce_href="http://research.microsoft.com/"&gt;Microsoft Research&lt;/A&gt;, where I manage the &lt;A href="http://research.microsoft.com/srr/" mce_href="http://research.microsoft.com/srr/"&gt;Software Reliability Research&lt;/A&gt; group in the &lt;A href="http://research.microsoft.com/rise/" mce_href="http://research.microsoft.com/rise/"&gt;Research in Software Engineering&lt;/A&gt; area.&lt;/P&gt;
&lt;P&gt;On behalf of the &lt;A href="http://research.microsoft.com/chess/" mce_href="http://research.microsoft.com/chess/"&gt;CHESS&lt;/A&gt; team, I am happy to announce our first &lt;A href="http://msdn.microsoft.com/devlabs/" mce_href="http://msdn.microsoft.com/devlabs/"&gt;DevLabs&lt;/A&gt; pre-release of the &lt;A href="http://msdn.microsoft.com/en-us/devlabs/cc950526.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/devlabs/cc950526.aspx"&gt;CHESS tools&lt;/A&gt; (build 0.1.30106.5) for finding subtle concurrency errors in &lt;I&gt;multithreaded single-process&lt;/I&gt; &lt;I&gt;Windows and .NET&lt;/I&gt; programs.&lt;/P&gt;
&lt;P&gt;CHESS is specifically designed for concurrency unit-testing and requires that you provide a set of test functions, each testing a particular concurrency scenario in the program. CHESS exhaustively enumerates all thread schedules of a test function by systematically inserting &lt;I&gt;preemptions&lt;/I&gt; (unplanned interruptions of a thread) at various points in a program’s execution.&lt;/P&gt;
&lt;P&gt;CHESS is realized as a test host for &lt;A href="http://msdn.microsoft.com/vsts2008/" target=_blank mce_href="http://msdn.microsoft.com/vsts2008/"&gt;Visual Studio Team System 2008&lt;/A&gt;, as well as a set of command-line tools for analyzing .NET and unmanaged code. CHESS also includes a simple graphical user interface for exploring error traces of concurrent programs called Concurrency Explorer. &lt;/P&gt;
&lt;P&gt;This post gives a glimpse of the Visual Studio Team System 2008 integration. Later posts will describe the command line tools. You can find out more about CHESS at our &lt;A href="http://research.microsoft.com/chess/" mce_href="http://research.microsoft.com/chess/"&gt;home page&lt;/A&gt; and &lt;A href="http://social.msdn.microsoft.com/Forums/en-us/chess/threads/" mce_href="http://social.msdn.microsoft.com/Forums/en-us/chess/threads/"&gt;MSDN forum&lt;/A&gt;.&lt;/P&gt;
&lt;H3&gt;Exploring Thread Schedules with CHESS&lt;/H3&gt;
&lt;P&gt;We provide a test host ([HostType(“Chess”)]) for Visual Studio Team System 2008 that runs &lt;I&gt;managed&lt;/I&gt; unit tests under control of CHESS. Let’s take a quick look at a test of a bank Account class that is supposed to be thread-safe (you can find the code for Account at the end of this post):&lt;/P&gt;&lt;PRE class=csharpcode&gt;    [TestClass]
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; TestBank
    {
        [TestMethod]
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; WithdrawAndDepositConcurrently()
        {
            var account = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Account(10);
            var child = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Thread(
               o =&amp;gt; { (o &lt;SPAN class=kwrd&gt;as&lt;/SPAN&gt; Account).Withdraw(2); }
               );
            child.Start(account);
            account.Deposit(1);
            child.Join();

            Assert.AreEqual&amp;lt;&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt;&amp;gt;(9, account.Read());
        }
    }&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/STYLE&gt;

&lt;P&gt;The attributes [TestClass] and [TestMethod] tell Visual Studio that the class TestBank and method WithdrawAndDepositConcurrently are test code. The body of the method creates an Account instance with $10. It then creates a child thread that will withdraw $2 from the account. The main thread starts the child thread and concurrently deposits $1 in the same account and then waits for the child thread to complete. Of course, regardless of the thread schedule, we expect the account to contain $9 at the end, as asserted in the final statement. We ran this test and got the following output:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_6.png"&gt;&lt;IMG title=image style="BORDER-TOP-WIDTH: 0px; DISPLAY: inline; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=161 alt=image src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_thumb_2.png" width=652 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_thumb_2.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Should we be satisfied that the test passed?&lt;/EM&gt; Our answer is “definitely not!” This is because this test has no control over which thread schedule executes. To test the code with CHESS, we simply attribute the WithdrawAndDepositConcurrently method with the &lt;A href="http://msdn.microsoft.com/en-us/library/bb165766(VS.80).aspx#UseHostTypeAttribute" mce_href="http://msdn.microsoft.com/en-us/library/bb165766(VS.80).aspx#UseHostTypeAttribute"&gt;HostType&lt;/A&gt; attribute, as shown below:&lt;/P&gt;&lt;PRE class=csharpcode&gt;        [TestMethod]
        [HostType(&lt;SPAN class=str&gt;"Chess"&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; WithdrawAndDepositConcurrently()
        {&lt;/PRE&gt;
&lt;P&gt;Running the test again, we see the following:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_4.png"&gt;&lt;IMG title=image style="BORDER-TOP-WIDTH: 0px; DISPLAY: inline; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=159 alt=image src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_thumb_1.png" width=644 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_thumb_1.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;We ended up with $8 instead of $9 - not good! If we run the test again, we get exactly the same result. This is because CHESS explores thread schedules in a deterministic order. That is, CHESS does not randomly perturb the thread scheduling but instead systematically explores the thread schedules.&lt;/P&gt;
&lt;H3&gt;Reproducing a Buggy Thread Schedule with CHESS&lt;/H3&gt;
&lt;P&gt;For long running tests, the number of schedules that CHESS explores can be enormous: CHESS may explore thousands if not tens of thousands of schedules before finding an error. When CHESS does find an error, it records an ASCII representation of the thread schedule that led to the bug.&amp;nbsp; With this schedule, you can use CHESS to immediately reproduce the bug without waiting through the many bug-free schedules CHESS explored to find the bug. To access the CHESS repro, we double click on the test in the above pane to see:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_8.png"&gt;&lt;IMG title=image style="BORDER-TOP-WIDTH: 0px; DISPLAY: inline; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=647 alt=image src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_thumb_3.png" width=403 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSReleaseonMSDNDevLabs_C413/image_thumb_3.png"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The “Error Message” section details the nature of the error. CHESS uses “Standard Console Output” section to give you information about the test and how to reproduce the error. The section “Standard Console Error” section contains a set of attributes that will help you reproduce the error with CHESS. We click on the link to copy this section’s content to the clipboard and then paste the contents of the clipboard before the method WithdrawAndDepositConcurrently, so the code looks like:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image008_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image008_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=155 alt=clip_image008 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image008_thumb.jpg" width=536 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image008_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Note that the ChessScheduleString is in a region so you can hide it; it is not intended to be human-readable. The string contains the schedule of events in the thread schedule that caused the assertion violation. There are two new attributes to direct CHESS. The first (“ChessMode”) tells CHESS to reproduce the execution directed by the CHESS schedule string. (The other mode of CHESS is the default exploration mode in which CHESS enumerates the thread schedules.) &lt;/P&gt;
&lt;H3&gt;Debugging with CHESS&lt;/H3&gt;
&lt;P&gt;Now we will run the test under the control of the debugger, with CHESS controlling the schedule, to find the source of assertion violation. When debugging, the second attribute (“ChessBreak”) is active. This directive tells CHESS to break before each thread preemption (recall that a preemption is an unexpected context switch).&amp;nbsp; CHESS has a vocabulary of concurrency primitives that you can use when debugging. As shown below, the first breakpoint takes place just before the main thread is about to acquire a lock on the Account in order to perform the deposit:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image010_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image010_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=136 alt=clip_image010 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image010_thumb.jpg" width=445 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image010_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This is the spot of the first preemption which transfers control from the main thread to the child thread. We now hit F10 to jump to the next preemption, which takes place in the child thread:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image011_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image011_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=170 alt=clip_image011 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image011_thumb.jpg" width=446 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image011_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;The second breakpoint takes place just after the child thread has read the value of the Account into the local variable “temp” (which has value 10) but just before the child thread is about to acquire a lock on the Account in order to perform the withdrawal. The error in the code is immediately obvious, as the comment explains: there is a window of time after the read of the Account’s balance but before the withdrawal in which another thread can interrupt the child thread. &lt;/P&gt;
&lt;P&gt;We press F10 a few times to see that control returns to the main thread which performs the deposit (raising the balance to 11 dollars):&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image012_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image012_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=167 alt=clip_image012 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image012_thumb.jpg" width=429 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image012_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;The main thread then blocks in the call child.Join(), waiting for the child thread to continue:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image014_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image014_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=145 alt=clip_image014 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image014_thumb.jpg" width=433 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image014_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;We press F10 a few more times until the assignment statement of the child thread is highlighted, as shown below. Hovering over the variable “balance”, we see that the current balance is 11, reflecting the deposit of the parent thread:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image016_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image016_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=164 alt=clip_image016 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image016_thumb.jpg" width=431 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image016_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Hovering over the local variable “temp”, we see that its value is the old/stale value of balance (10): &lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image018_2.jpg" mce_href="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image018_2.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=160 alt=clip_image018 src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image018_thumb.jpg" width=426 border=0 mce_src="http://blogs.msdn.com/blogfiles/chess/WindowsLiveWriter/CHESSToolReleaseonMSDNDevLabs_EFCE/clip_image018_thumb.jpg"&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Oops! Running to completion, we will witness that the assertion fails (because Withdraw will subtract 2 from 10 to get 8). The complete code of the buggy Account class is:&lt;/P&gt;&lt;PRE class=csharpcode&gt;    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; Account
    {
        &lt;SPAN class=kwrd&gt;private&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; balance;

        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; Account(&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; amount)
        {
            balance = amount;
        }

        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Withdraw(&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; amount)
        {
            &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; temp = Read();
            &lt;SPAN class=rem&gt;// oops, temp could become stale if we are&lt;/SPAN&gt;
            &lt;SPAN class=rem&gt;// preempted here&lt;/SPAN&gt;
            &lt;SPAN class=kwrd&gt;lock&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;)
            {
                balance = temp - amount;
            }
        }

        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; Read()
        {
            &lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; temp;
            &lt;SPAN class=kwrd&gt;lock&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;)
            {
                temp = balance;
            }
            &lt;SPAN class=kwrd&gt;return&lt;/SPAN&gt; temp;
        }

        &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; Deposit(&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; amount)
        {
            &lt;SPAN class=kwrd&gt;lock&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;this&lt;/SPAN&gt;)
            {
                balance = balance + amount;
            }
        }
    }&lt;/PRE&gt;
&lt;H3&gt;Don’t Stress, Test with CHESS!&lt;/H3&gt;
&lt;P&gt;Please &lt;A href="http://msdn.microsoft.com/devlabs/cc950526.aspx" mce_href="http://msdn.microsoft.com/devlabs/cc950526.aspx"&gt;download&lt;/A&gt; CHESS, try it out on your code and send us comments via our &lt;A href="http://social.msdn.microsoft.com/Forums/en-us/chess/threads/" mce_href="http://social.msdn.microsoft.com/Forums/en-us/chess/threads/"&gt;forum&lt;/A&gt;. Enjoy!&lt;/P&gt;
&lt;P&gt;- Tom Ball for the CHESS Team&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9301059" width="1" height="1"&gt;</description></item><item><title>Welcome to the CHESS blog</title><link>http://blogs.msdn.com/chess/archive/2009/01/08/welcome-to-the-chess-blog.aspx</link><pubDate>Fri, 09 Jan 2009 00:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9300227</guid><dc:creator>CHESS Tool</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/chess/comments/9300227.aspx</comments><wfw:commentRss>http://blogs.msdn.com/chess/commentrss.aspx?PostID=9300227</wfw:commentRss><description>&lt;P&gt;CHESS is a tool from Microsoft Research that helps you better test concurrent programs. You can use CHESS to systematically drive your programs into different thread interleavings, flushing out a lot of bugs in this process. Once a bug is found, CHESS can reproduce the interleaving for tremendously improving the debugging experience. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9300227" width="1" height="1"&gt;</description></item></channel></rss>