Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:50:37+00:00 2026-05-14T08:50:37+00:00

How do I prevent a race condition WITHOUT locking or using mutexes/semaphors in C++?

  • 0

How do I prevent a race condition WITHOUT locking or using mutexes/semaphors in C++? I’m dealing with a nested for loop in which I will be setting a value in an array:

for (int i = 0; i < m; ++i)
  for (int j = 0; j < n; ++j)
    for (int k = 0; k < o; ++k)
      array[k] += foo(...);

More or less, I want to deal with this so that I can ensure different threads running at the same time don’t write to array[k] at the same time. Any suggestions on how to approach this?

Edit: I am running on a Linux machine and I also have to use the Intel compiler. I will be using “icc” instead of “gcc” to compile the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:50:38+00:00Added an answer on May 14, 2026 at 8:50 am

    Assuming windows and that array contains elements of type LONG you could do something like:

    for (int i = 0; i < m; ++i) 
       for (int j = 0; j < n; ++j) 
          for (int k = 0; k < o; ++k)  {
              LONG val = foo(...);
              InterlockedAdd( &array[k], val);
          }
    

    If you’re not working in Windows your platform may have a similar set of APIs. As long as your platform has an InterlockedCompareExchange() type of API you can write your own version of InterlockedAdd().

    Something like the following (disclaimer – untested):

     int InterlockedAdd( int volatile* pDest, int operand)
     {
          int curval = *pDest;
          int oldval;
    
          do {
               oldval = curval;
               curval = InterlockedCompareExchange( pDest, oldval + operand, oldval);
          } while (curval != oldval);
    
          return oldval+operand;
     }
    

    As far as I know, Boost only has limited support for atomic/interlocked operations, apparently only enough to support atomic manipulation of reference counts. I don’t think that the support for interlocked operations in Boost is more than implementation detail (I’m currently dealing with an somewhat older version of Boost, so it’s possible that this isn’t the case anymore).

    There are some portable libraries that support atomic compare and exchange and other atomic operations as documented parts of the interface:

    • Apache APR: http://apr.apache.org/docs/apr/1.4/group__apr__atomic.html
    • glib: http://library.gnome.org/devel/glib/stable/glib-Atomic-Operations.html
    • Intel Thread Building Blocks: http://www.threadingbuildingblocks.org/

    Also note that C++0x will have support for atomic operations like compare/exchange – I’m not sure what the level of support is in current C++ compilers (it doesn’t appear to being VS 2010).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 402k
  • Answers 402k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Anyway, I needed to version the datamodel before changing anything… May 15, 2026 at 4:48 am
  • Editorial Team
    Editorial Team added an answer i += 4; means i = i + 4; //… May 15, 2026 at 4:48 am
  • Editorial Team
    Editorial Team added an answer So you want to remove the hash? Just set it… May 15, 2026 at 4:48 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.