c++线程传参

在C++中,可以使用std::thread的构造函数来向线程传递参数。这里有一个示例:

复制代码
#include <iostream>
#include <thread>

// 定义一个被线程调用的函数
void threadFunc(int arg1, double arg2, std::string arg3)
{
    std::cout << "arg1: " << arg1 << ", arg2: " << arg2 << ", arg3: " << arg3 << std::endl;
}

int main()
{
    // 创建一个线程,并传递参数
    std::thread t(threadFunc, 1, 3.14, "Hello, World!");

    // 等待线程结束
    t.join();

    return 0;
}

在这个例子中,定义了一个函数threadFunc,它接受三个参数。然后在main函数中创建了一个线程,并将这三个参数传递给了threadFunc

但是,如果的函数不接受参数,或者想要传递的参数数量不确定,可以使用std::refstd::cref来传递引用。例如:

复制代码
#include <iostream>
#include <thread>

// 定义一个被线程调用的函数
void threadFunc(int &arg1, double &arg2, std::string &arg3)
{
    std::cout << "arg1: " << arg1 << ", arg2: " << arg2 << ", arg3: " << arg3 << std::endl;
}

int main()
{
    int arg1 = 1;
    double arg2 = 3.14;
    std::string arg3 = "Hello, World!";

    // 创建一个线程,并传递参数引用
    std::thread t(threadFunc, std::ref(arg1), std::ref(arg2), std::ref(arg3));

    // 等待线程结束
    t.join();

    return 0;
}

在这个例子中,使用std::ref来传递变量的引用,这样就可以在threadFunc中修改这些变量的值。
std::refstd::cref 是 C++11 引入的,用于在 std::bind 或者 std::async 中引用成员函数或者非成员函数。这些函数主要在多线程中使用,目的是在函数调用中保持对象的引用,而不是复制对象。

  1. std::ref:
    std::ref 用于在函数绑定或异步函数调用中引用非const对象。这实际上允许通过引用而不是值来传递对象。例如,在多线程中,如果想在另一个线程中调用一个对象的成员函数,并且这个对象不是常量的,可以使用 std::ref 来传递这个对象的引用。

    示例:

    复制代码
    std::thread t(func, std::ref(myObj));
  2. std::cref:
    std::cref 类似于 std::ref,但它是用于引用const对象的。这意味着将通过const引用传递对象,不能在被调用的函数中修改这个对象。这在希望保证对象不会被改变时很有用。

    示例:

    复制代码
    std::thread t(func, std::cref(myObj));

这两个函数都定义在 <functional> 头文件中,因此在使用它们之前,必须包含这个头文件。它们实际上返回一个 reference_wrapper 对象,可以将这个对象传递给 std::bind 或 std::async,或者在其他需要函数调用的地方使用。

在C++中,可以使用std::thread的构造函数来向线程传递参数。这里有一个示例:

复制代码
#include <iostream>
#include <thread>

// 定义一个被线程调用的函数
void threadFunc(int arg1, double arg2, std::string arg3)
{
    std::cout << "arg1: " << arg1 << ", arg2: " << arg2 << ", arg3: " << arg3 << std::endl;
}

int main()
{
    // 创建一个线程,并传递参数
    std::thread t(threadFunc, 1, 3.14, "Hello, World!");

    // 等待线程结束
    t.join();

    return 0;
}

在这个例子中,定义了一个函数threadFunc,它接受三个参数。然后在main函数中创建了一个线程,并将这三个参数传递给了threadFunc

但是,如果的函数不接受参数,或者想要传递的参数数量不确定,可以使用std::refstd::cref来传递引用。例如:

复制代码
#include <iostream>
#include <thread>

// 定义一个被线程调用的函数
void threadFunc(int &arg1, double &arg2, std::string &arg3)
{
    std::cout << "arg1: " << arg1 << ", arg2: " << arg2 << ", arg3: " << arg3 << std::endl;
}

int main()
{
    int arg1 = 1;
    double arg2 = 3.14;
    std::string arg3 = "Hello, World!";

    // 创建一个线程,并传递参数引用
    std::thread t(threadFunc, std::ref(arg1), std::ref(arg2), std::ref(arg3));

    // 等待线程结束
    t.join();

    return 0;
}

在这个例子中,使用std::ref来传递变量的引用,这样就可以在threadFunc中修改这些变量的值。
std::refstd::cref 是 C++11 引入的,用于在 std::bind 或者 std::async 中引用成员函数或者非成员函数。这些函数主要在多线程中使用,目的是在函数调用中保持对象的引用,而不是复制对象。

  1. std::ref:
    std::ref 用于在函数绑定或异步函数调用中引用非const对象。这实际上允许通过引用而不是值来传递对象。例如,在多线程中,如果想在另一个线程中调用一个对象的成员函数,并且这个对象不是常量的,可以使用 std::ref 来传递这个对象的引用。

    示例:

    复制代码
    std::thread t(func, std::ref(myObj));
  2. std::cref:
    std::cref 类似于 std::ref,但它是用于引用const对象的。这意味着将通过const引用传递对象,不能在被调用的函数中修改这个对象。这在希望保证对象不会被改变时很有用。

    示例:

    复制代码
    std::thread t(func, std::cref(myObj));

这两个函数都定义在 <functional> 头文件中,因此在使用它们之前,必须包含这个头文件。它们实际上返回一个 reference_wrapper 对象,可以将这个对象传递给 std::bind 或 std::async,或者在其他需要函数调用的地方使用。

相关推荐
tan180°3 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
彭祥.4 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
lzb_kkk5 小时前
【C++】C++四种类型转换操作符详解
开发语言·c++·windows·1024程序员节
胖大和尚7 小时前
clang 编译器怎么查看在编译过程中做了哪些优化
c++·clang
钱彬 (Qian Bin)8 小时前
一文掌握Qt Quick数字图像处理项目开发(基于Qt 6.9 C++和QML,代码开源)
c++·开源·qml·qt quick·qt6.9·数字图像处理项目·美观界面
双叶8368 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
源代码•宸9 小时前
C++高频知识点(二)
开发语言·c++·经验分享
jyan_敬言10 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
liulilittle10 小时前
SNIProxy 轻量级匿名CDN代理架构与实现
开发语言·网络·c++·网关·架构·cdn·通信
tan77º11 小时前
【Linux网络编程】Socket - UDP
linux·服务器·网络·c++·udp