C++11/14 线程调用类对象和线程传参的方法

 更新时间:2019年01月22日 15:08:31   作者:lesliefish  
这篇文章主要介绍了C++11/14 线程调用类对象和线程传参的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

线程调用类对象

在前面的示例中,我们为线程任务使用了通常的函数。实际上,我们可以使用任何可调用对象或者lambda函数,如下调用类对象的例子:

#include <iostream>
#include <thread>

class MyFunctor
{
public:
  void operator()() 
  {
    std::cout << "functor\n"; 
  }
};

int main()
{
  MyFunctor fnctor;
  std::thread t(fnctor);
  std::cout << "main thread\n";
  t.join();
  return 0;
}

 在这里,我们创建了一个函数对象,并将其分配给线程任务,我们可能会用这种方法尝试此实例:

// MyFunctor fnctor;
std::thread t(MyFunctor());

但是编译不通过,所以,如果我们想让它工作,我们应该这样做:

// MyFunctor fnctor;
std::thread t((MyFunctor()));

就是说我们必须添加 ()包含 MyFunctor().

为什么?我这边不去深究,它跟C++函数声明规定相关。

线程传参

下面是将参数传递给线程的示例。 在这个例子中,我们传递一个字符串:

#include <iostream>
#include <thread>
#include <string>

void thread_function(std::string s)
{
  std::cout << "thread function ";
  std::cout << "message is = " << s << std::endl;
}

int main()
{
  std::string s = "Kathy Perry";
  std::thread t(&thread_function, s);
  std::cout << "main thread message = " << s << std::endl;
  t.join();
  return 0;
}

从下面的输出中,我们知道字符串已经成功地传递给了线程函数。

thread function message is = Kathy Perry
main thread message = Kathy Perry

如果我们想以引用方式传递,我们可能会这样做:

void thread_function(std::string &s)
{
  std::cout << "thread function ";
  std::cout << "message is = " << s << std::endl;
  s = "Justin Beaver";
}

为确保字符串真的是以引用方式传递,我们在线程函数尾部修改了字符串的内容。但是输出并没有改变:

thread function message is = Kathy Perry
main thread message = Kathy Perry

实际上,字符串是以值传递的而不是引用传递。为了以引用方式传递,我们应该使用std::ref稍微修改下代码:

std::thread t(&thread;_function, std::ref(s));

然后,修改后的输出为:

thread function message is = Kathy Perry
main thread message = Justin Beaver

有另一种免复制、非内存共享的方法在线程间传递参数。 我们可以使用 move():

std::thread t(&thread_function, std::move(s));

当字符串被从main函数移动到线程函数后,main函数就不再有这个变量了,输出为空值:

thread function message is = Kathy Perry
main thread message =

线程复制和移动 copy / move

如下代码期望拷贝线程是编译不通过的:

#include <iostream>
#include <thread>

void thread_function()
{
  std::cout << "thread function\n";
}

int main()
{
  std::thread t(&thread;_function);
  std::cout << "main thread\n";
  std::thread t2 = t;

  t2.join();

  return 0;
}

但是我们可以通过move把其所有权转移,见如下代码:

// t5.cpp
#include <iostream>
#include <thread>

void thread_function()
{
  std::cout << "thread function\n";
}

int main()
{
  std::thread t(&thread;_function);
  std::cout << "main thread\n";
  std::thread t2 = move(t);

  t2.join();

  return 0;
}

程序输出:

$ g++ t5.cpp -o t5 -std=c++11 -pthread
$ ./t5
main thread
thread function 

线程ID

我们可以通过 this_thread::get_id()获取线程的id信息:

int main()
{
  std::string s = "Kathy Perry";
  std::thread t(&thread_function, std::move(s));
  std::cout << "main thread message = " << s << std::endl;

  std::cout << "main thread id = " << std::this_thread::get_id() << std::endl;
  std::cout << "child thread id = " << t.get_id() << std::endl;

  t.join();
  return 0;
}

输出:

thread function message is = Kathy Perry
main thread message =
main thread id = 1208
child thread id = 5224

创建多少线程呢?

线程库提供了线程数量的建议函数hardware_concurrency():

int main()
{
  std::cout << "Number of threads = " 
       << std::thread::hardware_concurrency() << std::endl;
  return 0;
}

输出:

Number of threads = 2

Lambda函数

既然我们谈的C++,那么让我们来了解下Lambda。

我们可以用lambda函数(匿名函数)这样替换线程函数:

int main()
{
  std::thread t([]()
  {
    std::cout << "thread function\n";
  }
  );
  std::cout << "main thread\n";
  t.join();   // main thread waits for t to finish
  return 0;
}

注意,我们正在编写内联代码,并将其传递到另一个线程构造函数中。

Lambda表达式是用括号括起来的一系列语句, 前缀用[], 调用lambda编译接口告诉编译器我们正在声明一个lambda函数, 在我们的例子中,没有传递参数。我们本质上可以用 {} 作为一个任务 , 并把它分配给我们的线程。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • C语言中的5种简单排序算法(适合小白)

    C语言中的5种简单排序算法(适合小白)

    在编程练习时我们经常会遇到一些将一串乱序的数字排列成有序的数列(递增,递减)的问题,以此起到解决问题的效果,下面这篇文章主要给大家介绍了关于C语言中的5种简单排序算法的相关资料,需要的朋友可以参考下
    2023-03-03
  • c语言中的移位运算符

    c语言中的移位运算符

    这篇文章主要介绍了c语言中的移位运算符,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • C语言 数据类型详细介绍

    C语言 数据类型详细介绍

    本文主要讲解C语言 数据类型,这里整理了详细的数据类型的资料,希望能帮助刚刚开始学习C语言的同学
    2016-08-08
  • vs2019安装及简单处理技巧(超详细)

    vs2019安装及简单处理技巧(超详细)

    这篇文章主要介绍了vs2019安装及简单处理方法,本文是一篇非常详细的教程,通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • VC6.0代码自动提示 VC6.0在win7环境下代码提示智能化

    VC6.0代码自动提示 VC6.0在win7环境下代码提示智能化

    作为程序猿的你,是否已经喜欢或习惯依赖IDE开发环境呢,有了IDE环境,即使你想不起方法全名,只要知道某个前缀,或哪怕在提示列表中,一一查询,也可以找到自己想找的方法或属性
    2013-01-01
  • ubuntu系统下C++调用matlab程序的方法详解

    ubuntu系统下C++调用matlab程序的方法详解

    学习c++与matlab混合编程一般是通过c++调用matlab函数,因为matlab具有强大的数学函数库,然而vc++具有界面设计灵活的优点,下面这篇文章主要给大家介绍了关于在ubuntu系统下C++调用matlab程序的方法,需要的朋友可以参考下。
    2017-08-08
  • C语言实现影院管理系统程序设计

    C语言实现影院管理系统程序设计

    这篇文章主要为大家详细介绍了C语言实现影院管理系统程序设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • C++ NFS挂载及挂载命令

    C++ NFS挂载及挂载命令

    这篇文章主要介绍了C++ NFS挂载,文中给大家提到了挂载NFS时常用的命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Visual Studio 2022最新版安装教程(图文详解)

    Visual Studio 2022最新版安装教程(图文详解)

    本文主要介绍了Visual Studio 2022最新版安装教程,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 详解编译器编译原理

    详解编译器编译原理

    这篇文章主要介绍了详解编译器编译原理的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论