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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:00:30+00:00 2026-05-31T18:00:30+00:00

Suppose I have a program that initializes a global variable for use by threads,

  • 0

Suppose I have a program that initializes a global variable for use by threads, like the following:

int ThreadParameter;

// this function runs from the main thread
void SomeFunction() {
    ThreadParameter = 5;

    StartThread(); // some function to start a thread
    // at this point, ThreadParameter is NEVER modified.
}

// this function is run in a background worker thread created by StartThread();
void WorkerThread() {
    PrintValue(ThreadParameter); // we expect this to print "5"
}

These questions should apply for any generic processor architecture that one might encounter. I want the solution to be portable – not specific to an architecture with stronger memory guarantees, like x86.

  1. General question: despite being very common, is this really safe across all processor architectures? How to make it safe, if not?
  2. The global variable isn’t volatile; is it possibly going to be reordered after the StartThread() call and leave me hosed? How to fix this problem?
  3. Suppose the computer has two processors that have their own caches. Main thread runs on first processor and worker thread runs on second processor. Suppose the memory block that contains ThreadParameter has been paged into each processor’s cache before the program begins to run SomeFunction(). SomeFunction() writes 5 to ThreadParameter, which gets stored in the first processor’s cache, and then starts the worker thread, which runs on the second processor. Won’t WorkerThread() on the second processor see uninitialized data for ThreadParameter instead of the expected value of 5, since the memory page in the second processor hasn’t yet seen the update from the first processor?
  4. If something different is required – how best to handle this given that rather than a simple int, I could be working with a pointer to much more complex data types that aren’t necessarily used in a multithreaded environment?

If my concerns are unfounded, what are the specific reasons why I don’t need to worry?

  • 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-31T18:00:32+00:00Added an answer on May 31, 2026 at 6:00 pm

    From you description, it seems you’re writing to ThreadParameter (or some other data structure) BEFORE starting any child threads, and you will never write to ThreadParameter again… it exists to be read as needed, but never changed again after its initialization; is that correct? If so, then there’s no need whatsoever to employ any thread synchronization system calls (or processor/compiler primitives) every time a child thread wants to read the data, or even the first time for that matter.

    The treatment of volatile is somewhat compiler-specific; I know that at least with Diab for PowerPC, there is a compiler option regarding the treatment of volatile: either use the PowerPC EIEIO (or MBAR) instruction after every read/write to a variable, or don’t use it… this is in addition to prohibiting compiler optimizations associated with the variable. (EIEIO/MBAR is PowerPC’s instruction for prohibiting reordering of I/O by the processor itself; i.e, all I/O from before the instruction must complete before any I/O after the instruction).

    From a correctness/safety standpoint, it doesn’t hurt to declare it as volatile. But from a pragmatic standpoint, if you initialize ThreadParameter far enough ahead of StartThread(), declaring it volatile shouldn’t really be necessary (and not doing so would speed up all subsequent accesses of it). Pretty much any substantial function call (say, perhaps to printf() or cout, or any system call, etc) would issue orders of magnitude more instructions than necessary to ensure there’s no way the processor wouldn’t have long ago handled the write to ThreadParameter before your call to StartThread(). Realistically, StartThread() itself almost certainly will execute enough instructions before the thread in question actually starts. So I’m suggesting that you don’t really need to declare it volatile, probably not even if you initialize it immediately before calling StartThread().

    Now as to your question regarding what would happen if the page containing that variable were already loaded into the cache of both processors before the processor running the main thread performs the initialization: If you’re using a commonly available general purpose platform with like-kind CPUs, the hardware should already be in place to handle the cache coherency for you. The place you get into trouble with cache coherency on general purpose platforms, whether or not they’re multiprocessor, is when your processor has separate instruction & data caches and you write self-modifying code: The instructions written to memory are indistinguishable from data, so the CPU doesn’t invalidate those locations in the instruction cache, so there may be stale instructions in the instruction cache unless you subsequently invalidate those locations in the instruction cache (either issuing your own processor-specific assembly instructions, which you might not be allowed to do depending on your OS and your thread’s privilege level, or else issuing the appropriate cache-invalidate system call for your OS). But what you’re describing isn’t self-modifying code, so you should be safe in that regard.

    Your question 1 asks how to make this safe across ALL processor architectures. Well, as I discussed above, you should be safe if you’re using like-kind processors whose data busses are properly bridged. General-purpose processors designed for multiprocessor interconnection have bus snoop protocols to detect writes to shared memory… as long as your threading library properly configures the shared memory region. If you’re working in an embedded system, you may have to configure that yourself in your BSP… for PowerPC, you need to look at the WIMG bits in your MMU/BAT configuration; I’m unfamiliar with other architectures to give you pointers on those. BUT…. If your system is homebrew or if your processors are not like-kind, you may not be able to count on the two processors being able to snoop each others’ writes; check with your hardware folks for advice.

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

Sidebar

Related Questions

Suppose I have a function in a single threaded program that looks like this
Suppose I have the following program: #include <stdio.h> int main() { printf(This is a
Suppose that I have a Java program within an IDE (Eclipse in this case).
Suppose I have a big program that consists of hundreds of methods in it.
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a C++ program that has excessively deep inheritance as follows: using
Suppose I have 5 GLSL shaders that I successfully loaded in my opengl program.
I have a program that runs in the Win32 environment. There is one variable
Suppose I have one program that I compile and generate a preprocessed output file,
Suppose you have a disease diagnosis Prolog program that starts with many relations between

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.