Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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++ 为什么可以';使用std::thread通过引用发送对象_C++_Multithreading_Reference - Fatal编程技术网

C++ 为什么可以';使用std::thread通过引用发送对象

C++ 为什么可以';使用std::thread通过引用发送对象,c++,multithreading,reference,C++,Multithreading,Reference,我的代码是这样的:- #include <iostream> #include <thread> using namespace std; void swapno (int &a, int &b) { int temp=a; a=b; b=temp; } int main() { int x=5, y=7; cout << "x = " << x << "\ty = " <&

我的代码是这样的:-

#include <iostream>
#include <thread>
using namespace std;
void swapno (int &a, int &b)
{
    int temp=a;
    a=b;
    b=temp;
}
int main()
{
    int x=5, y=7;
    cout << "x = " << x << "\ty = " << y << "\n";
    thread t (swapno, x, y);
    t.join();
    cout << "x = " << x << "\ty = " << y << "\n";
    return 0;
}
#包括
#包括
使用名称空间std;
无效swapno(内部和a、内部和b)
{
内部温度=a;
a=b;
b=温度;
}
int main()
{
int x=5,y=7;

cout问题在于
std::thread
复制其参数并将其存储在内部。如果要通过引用传递参数,则需要使用or函数来创建引用包装器


问题是
std::thread
复制其参数并将其存储在内部。如果要通过引用传递参数,则需要使用or函数来创建引用包装器


您可以这样做:

    #include <iostream>
    #include <thread>
    void swapno (int *a, int *b)
    {
        int temp=*a;
        *a=*b;
        *b=temp;
    }
    int main()
    {
        int x = 5, y = 7;
        std::cout << "x = " << x << "\ty = " << y << "\n";
        std::thread t (swapno, &x, &y);
        t.join();
        std::cout << "x = " << x << "\ty = " << y << "\n";
        return 0;
    }
#包括
#包括
无效swapno(整数*a,整数*b)
{
int temp=*a;
*a=*b;
*b=温度;
}
int main()
{
int x=5,y=7;

std::cout您可以这样做,而不是:

    #include <iostream>
    #include <thread>
    void swapno (int *a, int *b)
    {
        int temp=*a;
        *a=*b;
        *b=temp;
    }
    int main()
    {
        int x = 5, y = 7;
        std::cout << "x = " << x << "\ty = " << y << "\n";
        std::thread t (swapno, &x, &y);
        t.join();
        std::cout << "x = " << x << "\ty = " << y << "\n";
        return 0;
    }
#包括
#包括
无效swapno(整数*a,整数*b)
{
int temp=*a;
*a=*b;
*b=温度;
}
int main()
{
int x=5,y=7;

std::cout您可以通过使用
std::ref
(即
线程t(swapno,std::ref(x),std::ref(y));
)显式引用。您可以通过使用
std::ref
(即
线程t(swapno,std::ref(x),std::ref(y));
)显式引用。