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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:07:53+00:00 2026-05-26T05:07:53+00:00

Consider the following scenario: There exists a globally accessible variable F . Thread A

  • 0

Consider the following scenario:

  1. There exists a globally accessible variable F.
  2. Thread A repeatedly assigns a random value to F (without any regard to the previous value of F).
  3. Thread B does the same thing as thread A (independent of A).
  4. Thread C repeatedly reads the value of F (and say, prints it).

This is in C++ (Visual C++) on Windows on x64 architecture, multi-processor. F is of type bool and marked volatile, and none of the accesses are protected by any locks.

Question: Is there anything thread-unsafe about this scenario?

Assuming that the logical behavior of the code is valid, is there anything unsafe about the fact that multiple threads are reading and writing the values to the same location at the same time?

What guarantees can be made (across architectures, OSes, compilers) about the atomicity of reading from and writing to variables that are <= word-size on the platform? (I am assuming word-size is important…)

On a related note, what is an acceptible way of communicating between threads the state of completion of some operation (none of the threads are waiting for the operation to complete, they may just be interested in querying the state from time to time)?

  • 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-26T05:07:53+00:00Added an answer on May 26, 2026 at 5:07 am

    It depends on your thread-safety requirements. You’ll always get a consistent value (that is, it is impossible that you get half the value of thread A’s write and the other half from thread B), but there is no guarantee that the value you’ll read is, in fact, the latest one that was logically written.

    The problem here is the CPU cache that may or may not get flushed. When a thread writes to the memory, the value first goes to the cache, and eventually it gets written to memory. In the while, if other cores attempt to read the object from memory, they’ll get the old value.

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

Sidebar

Related Questions

Consider the following scenario. We have a C++ function with a static local variable:
I have some queries with following scenario on github. consider there is one organization
Please consider the following scenario, There are two web applications App1 & App2. A
Consider the following scenario: There are three kinds of entities, say Foo , Bar
Consider the following MySQL scenario: Table1: id, date, value Table2: id, month, count Table
Consider following scenario: I have RESTful URL /articles that returns list of articles user
Consider the following scenario . I have an array of numbers: [ 1,2,3,4 ]
Please consider the following scenario: map(T,S*) & GetMap(); //Forward decleration map(T, S*) T2pS =
Let's consider the following scenario: a function which can generate code colors from white
From couple of days i am thinking of a following scenario Consider I have

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.