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,或者在其他需要函数调用的地方使用。

相关推荐
虾球xz1 小时前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz1 小时前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
虾球xz6 小时前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
fpcc7 小时前
跟我学c++高级篇——模板元编程之十三处理逻辑
c++
格林威7 小时前
Baumer工业相机堡盟工业相机的工业视觉中为什么偏爱“黑白相机”
开发语言·c++·人工智能·数码相机·计算机视觉
Dream it possible!8 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
Dddle110 小时前
C++:this指针
java·c语言·开发语言·c++
不見星空10 小时前
2025年第十六届蓝桥杯软件赛省赛C/C++大学A组个人解题
c语言·c++·蓝桥杯
jiunian_cn10 小时前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
梁下轻语的秋缘10 小时前
每日c/c++题 备战蓝桥杯(洛谷P1387 最大正方形)
c语言·c++·蓝桥杯