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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:03:26+00:00 2026-06-02T09:03:26+00:00

I have a shared variable of type double . This variable will be accessed

  • 0

I have a shared variable of type double. This variable will be accessed by two threads. One thread will ever only write the variable, whereas the other thread will ever only read the variable.

Do I still get race condition here? If yes, is there an “easy” way in C++ to implement atomic access? How do I implement it efficiently if there is going to be much much more reads than writes? Do I need to mark the variable as volatile?

EDIT: OK the “reader” thread works periodically on batches of data and the propagation of the new value is not time-sensitive. Instead of implementing complicated interlocking that I have no good way to test, I can just declare another temp variable the writer thread will write to. Then when the reader is finished with one batch, it can atomically propagate the temp value to the actual variable. Would that be race-condition-free?

  • 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-02T09:03:28+00:00Added an answer on June 2, 2026 at 9:03 am

    Yes, there’s a race condition, since double variables are not atomic on most processors.

    Use 3 doubles (possibly an array with extra padding in between, to avoid false sharing that kills performance).

    One is owned by the reader, one is owned by the writer, one is being handed off.

    To write: write to the write slot, then atomically swap (e.g. with InterlockedExchange) the pointer/index for the write slot with the index for the handoff slot. Since the index is pointer-sized or smaller, atomically swapping is easy as long as the variable is properly aligned. If by chance your platform offers interlocked-exchange with and without memory barriers, use the one with.

    To read: atomically swap the pointer/index for the read slot with the index for the handoff variable. Then read the read slot.

    You should actually include a version number also, since the read thread will tend to bounce between the latest and previous slot. When reading, read both before and after the swap, then use the one with the later version.

    Or, in C++11, just use std::atomic.

    Warning: the above only works for single writer/single reader (the particular case in this question). If you have multiple, think about a reader-writer lock or similar protecting all access to the variable.

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

Sidebar

Related Questions

I have 2 threads and a shared float global. One thread only writes to
I have a variable named as [ThreadStatic] , will it shared across 2 app
If you have a multi-threaded application with a word variable shared between threads (ie
I have a component that has a sub-component they both use a shared variable
I have an object which is shared by multiple threads, I want to lock
I have a certain object type that is stored in a database. This type
m_io_service.post(boost::ref(i)); I have this call in a section of code, the underlying type i
I have this code copied from one of questions from SO: public static String
I have shared hosting on CentOS with local MySQL database. I'm able to run
I have created a multi module maven project. Now I have shared the project

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.