Class 成员在构造函数后更改地址(线程)

Class member changing address after constructor (thread)

我正在尝试将 class A 的 class 成员作为常量引用存储在 class B(由 A 控制)中,以便我只能修改值通过 class A 但 B 仍然可以读取它。问题是 class A 成员的地址在构造函数完成后发生变化,因此 class B 中的引用成员将不再指向 class A 成员。

每个对象A在一个单独的线程中运行。当我不使用多线程时,我不会得到不同的地址,所以我知道这是导致问题的原因,但我不知道出了什么问题。

#include <thread>
#include <vector>
#include <iostream>

using namespace std;

class B
{
private:
    const float& m_foo;
public:
    friend class A;
    B(const float& foo):
        m_foo(foo)
    {
        cout << "m_b.m_foo address in B constructor:\t" << &m_foo << endl;
    }
};

class A {
private:
    float m_foo;
    B m_b; 

public:
    A(float foo):
        m_foo(foo),
        m_b(m_foo)
    {
        cout << "m_a.m_foo address in A constructor:\t" << &m_foo << endl;
        cout << "m_b.m_foo address in A constructor:\t" << &(m_b.m_foo) << endl;
    }
    void run()
    {
        cout << "m_a.m_foo address after A and B constructor:\t" << &m_foo << endl;
        cout << "m_b.m_foo address after A and B constructor:\t" << &(m_b.m_foo) << endl;
    }
};

int main(int argc, char *argv[]){

        int n = 1;
        vector<A> vector_A;
        vector<thread> threads;
        vector_A.reserve(n);

        for (int i=0; i<n; i++){
            // Store A objects inside vector_A
            vector_A.push_back(A(10));
            // Store the threads inside the threads vector
            threads.push_back(thread(&A::run,
                std::ref(vector_A[i])));}
        for (int i=0; i<n; i++){
            // Wait for the rest of the thread to finish
            threads[i].join();
        }

    return 0;
}

输出:

m_b.m_foo address in B constructor:     0x7ffee160d708
m_a.m_foo address in A constructor:     0x7ffee160d708
m_b.m_foo address in A constructor:     0x7ffee160d708
m_a.m_foo address after A and B constructor:    0x7faab8c02af0
m_b.m_foo address after A and B constructor:    0x7ffee160d708

你可以使用这样的东西:(对我有用)

  • 添加函数初始化 A 对象并调用 run:
void newA_run(vector<A>& v, float val){
    // Store A objects inside vector_A
    A a(val);
    v.push_back(a);

    a.run();
}
  • 像这样编辑主体:
int main(int argc, char *argv[]){

        int n = 1;
        vector<A> vector_A;
        vector<thread> threads;
        vector_A.reserve(n);

        for (int i=0; i<n; i++){
            // Store the threads inside the threads vector
            //threads.push_back(thread(&A::run, std::ref(vector_A[i])));}
            threads.push_back(thread(&newA_run,
                                     std::ref(vector_A),
                                     10));
        }
        for (int i=0; i<n; i++){
            // Wait for the rest of the thread to finish
            threads[i].join();
        }

    return 0;
}

更新:

有一个更简单的方法:

  • 只编辑 main 中的 vector_A 如下:
int main(int argc, char *argv[]){

    int n = 1;
    A* vector_A[n];// = (A*) malloc(n * sizeof(A));
    vector<thread> threads;

    for (int i=0; i<n; i++){
        // Store A objects inside vector_A
        vector_A[i] = new A(10);
        // Store the threads inside the threads vector
        threads.push_back(thread(&A::run,
            std::ref( *vector_A[i] )));}
    for (int i=0; i<n; i++){
        // Wait for the rest of the thread to finish
        threads[i].join();

        // I put public 'm_foo' in both classes and 'm_b', to check after join
        // and it works fine.
        cout << "m_a.m_foo address after join:\t" << &(vector_A[i]->m_foo) << endl;
        cout << "m_b.m_foo address after join:\t" << &(vector_A[i]->m_b.m_foo) << endl;
    }

    return 0;
}

希望对你有帮助。