Ask a questionAsk a question
 

AnswerCode with no lock statements

  • Wednesday, October 07, 2009 2:12 PMSteven J Walker Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Currently my code has no lock statements - I want to use CHESS too detect where I need them.

    I modified the Account class in the Bank example and removed all the lock statements. This resulted in the chess test passing every time, compared to stress tests which sometimes (very few) discover the bug.

    So if I don't have lock statements CHESS won't work?

    The following is the code of the account class:

    public class Account
    {
        private int balance;
    
        public Account(int amount)
        {
            balance = amount;
        }
    
        public void Withdraw(int amount)
        {
            balance = balance - amount;
        }
    
        public int Read()
        {
            return balance;
        }
    
        public void Deposit(int amount)
        {
            balance = balance + amount;
        }
    }
    

    This is the test code:

    [TestMethod]
    [HostType("Chess")]
    public void WithdrawAndDepositConcurrently()
    {
        var account = new Account(10);
        var child = new Thread(
           o => { (o as Account).Withdraw(2); }
           );
        child.Start(account);
        account.Deposit(1);
        child.Join();
    
        Assert.AreEqual<int>(9, account.Read());
    }
    
    [TestMethod]
    public void WithdrawAndDepositStress10000()
    {
        for (int i = 0; i < 10000; i++)
            WithdrawAndDepositConcurrently();
    }
    
    [TestMethod]
    public void WithdrawAndDepositStress100000()
    {
        for (int i = 0; i < 100000; i++)
            WithdrawAndDepositConcurrently();
    }
    


    Most of the time, only the last test discovers the bug.

    Best Regards,
    Walker

Answers

  • Thursday, October 08, 2009 1:41 PMTom BallMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You need to use mchess and use /detectraces or /preemptaccesses.  By default, CHESS only inserts preemptions at synchronization points, so if you don't have any, then you need to either:

    (1) tell CHESS to detect data races (/detectraces);
    (2) tell CHESS to place a preemption before each read/write of shared memory (/preemptaccesses)

    Unfortunately, these two options only are available from the command line tool mchess. Best,

    -- Tom

All Replies

  • Thursday, October 08, 2009 1:41 PMTom BallMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You need to use mchess and use /detectraces or /preemptaccesses.  By default, CHESS only inserts preemptions at synchronization points, so if you don't have any, then you need to either:

    (1) tell CHESS to detect data races (/detectraces);
    (2) tell CHESS to place a preemption before each read/write of shared memory (/preemptaccesses)

    Unfortunately, these two options only are available from the command line tool mchess. Best,

    -- Tom

  • Thursday, October 08, 2009 1:48 PMSteven J Walker Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks a lot Tom!

    You know if there are plans to include this attributes as well as others (like the timeout) in VS in the near future?

    Best regards,
    Walker
  • Thursday, October 08, 2009 2:25 PMTom BallMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Walker,

    Unfortunately, we (a few researchers) don't have the resources to support both the CHESS engine and do a proper VS integration.   Since most of our users are internal and using the CHESS engine via the command line, that's where most of our time goes.  Thanks for the feedback.

    -- Tom