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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:19:16+00:00 2026-06-05T08:19:16+00:00

I would like to determine if the below code would allow the CPU to

  • 0

I would like to determine if the below code would allow the CPU to fetch the unsafe_variable twice? Let’s assume the compiler will not reorder or optimise the code because of the volatile and _ReadWriteBarrier (on VS). Mutex cannot be used here and I only care about the case of the potential double fetch.

I am not an expert in term of CPU design but what I am concerned about regarding a potential double fetch would be: Speculative execution (Performance optimization technique including branch prediction and prefetch techniques), Register and memory location renaming and the use of reorder buffer and store buffers within one or two CPUs? Please let me know if a double fetch here would be at all possible.

int function(void* Data) {
    size_t _varSize = ((volatile DATA *)Data)->unsafe_variable;
    // unsafe_variable is in some kind of shared memory and can change at any time
    _ReadWriteBarrier();
    // this does not prevent against CPU optimisations (MemoryBarrier would)
    if (_varSize > x * y) { return FALSE;}
    size_t size = _varSize - t * q;
    function_xy(size);
    return TRUE;
}
  • 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-05T08:19:17+00:00Added an answer on June 5, 2026 at 8:19 am

    Nothing in the C language specifies memory system behavior at this level nor anything about threads, so there is no certain answer.

    To be certain you would need exact details of the CPU, OS, and compiler.

    However, I doubt any modern architecture would fetch twice from memory to serve any of the purposes you mention, provided computation is not interrputed. Any reference after the first would be to cache.

    However, if there is a context switch after the unsafe_variable fetch is begun but before _varSize can be used, it’s imaginable that the fetch might be restarted when this thread continues.

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

Sidebar

Related Questions

Let's say I have the string Hey . I would like to determine all
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to determine what the long url of a short url is.
I would like to determine a remote page's encoding through detection of the Content-Type
I have two times in PHP and I would like to determine the elapsed
I'm currently writing a program in Python, and I would like to determine the
Would like to know the c# code to actually retrieve the IP type: Static
I would like to code a genetic algorithm that learns to play a game
I would like to run some unit tests on the method below I am
Would like to be able to set colors of headings and such, different font

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.