Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法理解*和&;当他们带着线来的时候_C++_Multithreading_Pointers_Pass By Reference - Fatal编程技术网

C++ 无法理解*和&;当他们带着线来的时候

C++ 无法理解*和&;当他们带着线来的时候,c++,multithreading,pointers,pass-by-reference,C++,Multithreading,Pointers,Pass By Reference,全部!我遇到了一些麻烦!函数“change”什么也没变 void change(int &a){ cout << "thread tt \t" << &a << " value:" << a << endl; a = 5; return; } int main() { int a = 4; cout << "Thread main \t" << &

全部!我遇到了一些麻烦!函数“change”什么也没变

void change(int &a){
    cout << "thread tt \t" << &a << " value:" << a << endl;
    a = 5;
    return;
}

int main()
{
    int a = 4;
    cout << "Thread main \t" << &a << " value:" << a << endl;
    thread tt(change, a);

    tt.join();
    cout << "After join() \t" << &a << " value:" << a << endl;

    return 0;
}
似乎只有当我们将函数“change”作为线程运行时,它才起作用

我想知道这是否与STACK有关


谁能给我解释一下吗?非常感谢。。它真的让我很痛苦,你的问题是std::thread没有引用
a
。您可以使用包装
a
,以获取对线程函数的引用

thread tt(change, std::ref(a));
thread change   0038FE98 value:1
0038FE98 2
thread change2  0038FD68 value:0038FE98
0038FE98 3
Thread main     0038FE98 value:3

thread change   0070FAF4 value:3
After t.join()  0038FE98 value:3
0038FE98 3

thread change2  00D0F99C value:0038FE98
After tt.join() 0038FE98 value:5
0038FE98 5
thread tt(change, std::ref(a));