C++11/14中线程如何调用类对象和线程传参

发布时间:2021-08-12 11:47:29 作者:小新
来源:亿速云 阅读:522

这篇文章将为大家详细讲解有关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++11/14中线程如何调用类对象和线程传参”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. c++中 seekg seekp tellg tellp 的简单用法
  2. iOS培训篇——C++预处理命令解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:python脚本中如何调用iftop统计业务应用流量

下一篇:PHP怎么删除Excel单元格指定列

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》