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 8061187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:15:07+00:00 2026-06-05T10:15:07+00:00

If I use a shared variable, let’s say a double, to calculate some kind

  • 0

If I use a shared variable, let’s say a double, to calculate some kind of sum along the execution of the program. Would that in anyway be vulnerable to non-stable operations? I mean, would it be possible that more than one core would access this variable in an asynchronous way and cause unstable results?

For example:
this is a global variable:

double totalTime = 0;

and in each core a command is called:

totalTime += elapsedTime;

This last operation/statement is executed by taking the value of totalTime, put it the the CPU register, and then do the addition. I can imagine that more than one core would take the same value at the same instant, and then add the new elapsedTime, and then the value stored in totalTime would be overwritten with the wrong value, due to latency. Is that possible? and how can I solve this?

Thank you.

  • 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-06-05T10:15:09+00:00Added an answer on June 5, 2026 at 10:15 am

    Clearly this operation is not thread-safe since, as you mentioned yourself, it involves several assembler instructions. In fact, openMP even has a special directive for this kind of operations.

    You will need the atomic pragma to make it, well, “atomic”:

    #pragma omp atomic
    totalTime += elapsedTime;
    

    Note that atomic only works when you have a single update to a memory location, like an addition, increment, etc.

    If you have a series of instructions that need to atomic together you must use the critical directive:

    #pragma omp critical
    {
        // atomic sequence of instructions
    }
    

    Edit: Here’s a good suggestion from “snemarch”: If you are repeatedly updating the global variable totalTime in a parallel loop you can consider using the reduction clause to automatize the process and also make it much more efficient:

    double totalTime = 0;
    
    #pragma omp parallel for reduction(+:totalTime)
    for(...)
    {
        ...
        totalTime += elapsedTime;
    }
    

    At the end totalTime will correctly contain the sum of the local elapsedTime values without need for explicit synchronization.

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

Sidebar

Related Questions

I have a component that has a sub-component they both use a shared variable
Let's say we have the following mega-simple Python script: print Initializing.... a=10 print Variable
Let's say I have this custom component. It subclasses JMenuItem and all instances use
What is the use of a Shared variable in VB.NET?
I am planning to use a shared variable to implement a logging facility. Have
today I wanted to use some variables in my shared/menu partial. I know that
Can I use shared memory on Windows in order to have a common memory
I want to use shared memory across processes and wanted to use the robust
I've got several programs that use shared libraries. Those shared libraries in turn use
How can I use std::shared_ptr for array of double? Additionally what are advantages/disadvantages of

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.