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

  • Home
  • SEARCH
  • 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 7818967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:45:00+00:00 2026-06-02T06:45:00+00:00

Derived from this question and related to this question : If I construct an

  • 0

Derived from this question and related to this question:

If I construct an object in one thread and then convey a reference/pointer to it to another thread, is it thread un-safe for that other thread to access the object without explicit locking/memory-barriers?

// thread 1
Obj obj;

anyLeagalTransferDevice.Send(&obj);
while(1); // never let obj go out of scope

// thread 2
anyLeagalTransferDevice.Get()->SomeFn();

Alternatively: is there any legal way to convey data between threads that doesn’t enforce memory ordering with regards to everything else the thread has touched? From a hardware standpoint I don’t see any reason it shouldn’t be possible.

To clarify; the question is with regards to cache coherency, memory ordering and whatnot. Can Thread 2 get and use the pointer before Thread 2’s view of memory includes the writes involved in constructing obj? To miss-quote Alexandrescu(?) “Could a malicious CPU designer and compiler writer collude to build a standard conforming system that make that break?”

  • 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-02T06:45:02+00:00Added an answer on June 2, 2026 at 6:45 am

    Reasoning about thread-safety can be difficult, and I am no expert on the C++11 memory model. Fortunately, however, your example is very simple. I rewrite the example, because the constructor is irrelevant.

    Simplified Example

    Question: Is the following code correct? Or can the execution result in undefined behavior?

    // Legal transfer of pointer to int without data race.
    // The receive function blocks until send is called.
    void send(int*);
    int* receive();
    
    // --- thread A ---
    /* A1 */   int* pointer = receive();
    /* A2 */   int answer = *pointer;
    
    // --- thread B ---
               int answer;
    /* B1 */   answer = 42;
    /* B2 */   send(&answer);
               // wait forever
    

    Answer: There may be a data race on the memory location of answer, and thus the execution results in undefined behavior. See below for details.


    Implementation of Data Transfer

    Of course, the answer depends on the possible and legal implementations of the functions send and receive. I use the following data-race-free implementation. Note that only a single atomic variable is used, and all memory operations use std::memory_order_relaxed. Basically this means, that these functions do not restrict memory re-orderings.

    std::atomic<int*> transfer{nullptr};
    
    void send(int* pointer) {
        transfer.store(pointer, std::memory_order_relaxed);
    }
    
    int* receive() {
        while (transfer.load(std::memory_order_relaxed) == nullptr) { }
        return transfer.load(std::memory_order_relaxed);
    }
    

    Order of Memory Operations

    On multicore systems, a thread can see memory changes in a different order as what other threads see. In addition, both compilers and CPUs may reorder memory operations within a single thread for efficiency – and they do this all the time. Atomic operations with std::memory_order_relaxed do not participate in any synchronization and do not impose any ordering.

    In the above example, the compiler is allowed to reorder the operations of thread B, and execute B2 before B1, because the reordering has no effect on the thread itself.

    // --- valid execution of operations in thread B ---
               int answer;
    /* B2 */   send(&answer);
    /* B1 */   answer = 42;
               // wait forever
    

    Data Race

    C++11 defines a data race as follows (N3290 C++11 Draft): “The execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. Any such data race results in undefined behavior.” And the term happens before is defined earlier in the same document.

    In the above example, B1 and A2 are conflicting and non-atomic operations, and neither happens before the other. This is obvious, because I have shown in the previous section, that both can happen at the same time.

    That’s the only thing that matters in C++11. In contrast, the Java Memory Model also tries to define the behavior if there are data races, and it took them almost a decade to come up with a reasonable specification. C++11 didn’t make the same mistake.


    Further Information

    I’m a bit surprised that these basics are not well known. The definitive source of information is the section Multi-threaded executions and data races in the C++11 standard. However, the specification is difficult to understand.

    A good starting point are Hans Boehm’s talks – e.g. available as online videos:

    • Threads and Shared Variables in C++11
    • Getting C++ Threads Right

    There are also a lot of other good resources, I have mentioned elsewhere, e.g.:

    • std::memory_order – cppreference.com
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is derived from: How to get this Method object via reflection? I'm
This question is derived from my previous SO question's commends . I am confused
This question is derived from the topic: vector reserve c++ I am using a
derived from this question , is it possible to use HQL or Criteria for
Starting from this code: class Base{ public: virtual void foo(){....} }; class Derived{ public:
I have a class derived from Dictionary. I need this class to simulate a
//This program is taken from http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/ #include <iostream> using namespace std; class A {
Simplified from this question and got rid of possible affect from LinqPad(no offsensive), a
This question is one of the big doubts that looms around my head and
I'm sorry for this noob question. There're 2 tables : the smaller one is

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.