Code with no lock statements
- 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
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- Marked As Answer byTom BallMSFT, ModeratorThursday, October 08, 2009 1:41 PM
All Replies
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- Marked As Answer byTom BallMSFT, ModeratorThursday, October 08, 2009 1:41 PM
- 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 - 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


