c++ 线程
创始人
2024-12-08 08:09:37
0

在 C++ 中,std::thread 构造函数可以用于将参数传递给线程。这里是一个基本的示例,展示了如何使用 std::thread 来传递参数:

#include  #include   // 定义一个被线程调用的函数 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  #include  #include  // std::ref 和 std::cref 需要这个头文件  // 定义一个被线程调用的函数 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::refstd::cref 的使用

  1. std::ref:
    std::ref 用于在函数绑定或异步函数调用中引用非 const 对象。例如:

    std::thread t(func, std::ref(myObj)); 
  2. std::cref:
    std::cref 类似于 std::ref,但它用于引用 const 对象。例如:

    std::thread t(func, std::cref(myObj)); 

这两个函数都定义在 头文件中,因此在使用它们之前,必须包含这个头文件。

多线程示例

以下是一个示例,展示了如何使用 std::threadstd::promise 进行线程同步:

#include  #include  #include  #include   int main() {     std::promise promise;     std::future future = promise.get_future();      std::thread t([&promise] {         std::string s = "hello";         std::this_thread::sleep_for(std::chrono::seconds(1));         promise.set_value(s);     });      t.join();     std::string value = future.get();     std::cout << value << std::endl;      return 0; } 

使用信号量的多线程示例

下面是一个使用信号量和互斥锁进行线程同步的示例:

#include  #include  #include  #include  #include   std::mutex mtx; sem_t sem; int counter = 0;  void increment_counter(int id) {     sem_wait(&sem);     std::lock_guard lock(mtx);     std::this_thread::sleep_for(std::chrono::milliseconds(100));     counter++;     std::cout << "Thread " << id << " incremented counter to " << counter << std::endl;     sem_post(&sem); }  void read_counter(int id) {     sem_wait(&sem);     std::lock_guard lock(mtx);     std::this_thread::sleep_for(std::chrono::milliseconds(100));     std::cout << "Thread " << id << " read counter value " << counter << std::endl;     sem_post(&sem); }  int main() {     sem_init(&sem, 0, 5);      std::vector threads;     for (int i = 0; i < 10; ++i) {         if (i % 2 == 0) {             threads.push_back(std::thread(increment_counter, i));         } else {             threads.push_back(std::thread(read_counter, i));         }     }      for (auto &thread : threads) {         thread.join();     }      sem_destroy(&sem);      return 0; } 

在这个示例中,使用了信号量和互斥锁来控制对共享资源 counter 的访问。这样可以确保多个线程安全地访问和修改共享资源。

相关内容

热门资讯

揭秘一下!新金龙辅助工具,金虎... 您好,金虎爷辅助插件这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这...
透视免费!潮友会破解器(辅助挂... 透视免费!潮友会破解器(辅助挂)一直有开挂辅助插件(有挂方法)1、玩家可以在潮友会破解器软件透明挂俱...
推荐攻略!新金龙辅助工具(辅助... 推荐攻略!新金龙辅助工具(辅助挂)最初有开挂辅助插件(有挂辅助)是一款可以让一直输的玩家,快速成为一...
盘点一款!微乐小程序辅助器脚本... 盘点一款!微乐小程序辅助器脚本,广东雀神智能插件有什么功能,揭秘教程(有挂猫腻)是一款可以让一直输的...
攻略讲解!蜜瓜大厅辅助免费下载... 攻略讲解!蜜瓜大厅辅助免费下载,蛮王大厅辅助插件,详细教程(真的有挂);攻略讲解!蜜瓜大厅辅助免费下...
推荐一款!呗兔互娱有没有挂(辅... 推荐一款!呗兔互娱有没有挂(辅助挂)好像有开挂辅助插件(有挂实锤)1、点击下载安装,呗兔互娱有没有挂...
玩家必知教程!潮汕掌上辅助挂定... 玩家必知教程!潮汕掌上辅助挂定制交易平台(辅助挂)果然有开挂辅助安装(有人有挂);潮汕掌上辅助挂定制...
透视讲解!微乐内蒙古自建房脚本... 透视讲解!微乐内蒙古自建房脚本免费下载,广东雀神胜率修改,透牌教程(详细教程)是一款可以让一直输的玩...
一分钟教会你!新九哥作弊,财神... 一分钟教会你!新九哥作弊,财神十三章怎样加强运气,教你攻略(有挂透视)是一款可以让一直输的玩家,快速...
透视免费!传送屋有没有挂(辅助... 透视免费!传送屋有没有挂(辅助挂)本来有开挂辅助插件(有挂辅助)传送屋有没有挂辅助器中分为三种模型:...