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

相关推荐
Dream it possible!2 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴2 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程2 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空3 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc7744 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白15 小时前
C++——list模拟实现
开发语言·c++
程序员老舅5 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初1115 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
cookies_s_s7 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
不想编程小谭7 小时前
力扣LeetCode: 2506 统计相似字符串对的数目
c++·算法·leetcode