DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

New C++ Concurrency Static Analysis Warnings in Visual Studio 2012

Sasha Goldshtein user avatar by
Sasha Goldshtein
·
Jul. 04, 12 · Interview
Like (0)
Save
Tweet
Share
5.13K Views

Join the DZone community and get the full member experience.

Join For Free

another cool feature in the visual studio 2012 c++ compiler is the revamped code analysis rule sets and brand new ui for configuring them. this is not just the simple /analyze switch we all know and love since visual studio 2005 anymore.

to get a general impression of the ui changes, open a c++ project’s properties and check out the code analysis node. you’ll be able to review the rule set that runs on your project and optionally enable/disable specific warnings. check out some of these rules:

image

specifically, i’d like to focus on the new concurrency analysis rules (in the c261 nn range). these rules—like most c++ code analysis rules—rely on annotations placed in the source code that instruct the analysis engine what to look for. for example, you can place a _guarded_by_(x) annotation on a variable to indicate that access to it should always be protected by a lock x .

carefully placing these annotations all over your source code might seem like an excessive burden, but this is an investment that pays off greatly when you later introduce subtle bugs. consider the following simple example of an account class, whose balance field is protected by the critical section lock :

class account {
private:
  critical_section lock;
  _guarded_by_(lock) double balance;
public:
  //…constructor and other methods omitted for brevity
  void withdraw(double amount) {
    entercriticalsection(&lock);
    balance –= amount;
    leavecriticalsection(&lock);
  }
};

sounds easy. now you have a new requirement, to make sure that the balance is not negative before withdrawing. you make the following careless change, and oops—there’s a race condition in your class!

bool withdraw(double amount) {
  if (balance < 0.0) return false; //oops!
  entercriticalsection(&lock);
  balance –= amount;
  leavecriticalsection(&lock);
  return true;
}

the code analysis engine will detect this problem and report it immediately :

image

image

there are many other use cases for these concurrency warnings: mark a variable with _interlocked_ and the compiler will make sure it’s always accessed with an interlocked operation ; failure to release a lock in some code path will always be triggered:

image

image

…and there’s the holy grail of deadlock detection, too, if you incorporate lock leveling into your code with the _create_lock_level_, _lock_level_order_ and _has_lock_level_ annotations . here’s an example:

image

image

this is definitely a step in the right direction for automatic code analysis in c++. you should at least once try running all the rules on your existing code base—you might be surprised how many nasty bugs code analysis could unveil!

Published at DZone with permission of Sasha Goldshtein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Software Developer Competencies: How To Recognize a Good Programmer
  • Key Elements of Site Reliability Engineering (SRE)
  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Create Spider Chart With ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: