C++11/14 线程调用类对象和线程传参的方法
作者:lesliefish 发布时间:2022-04-08 18:20:47
线程调用类对象
在前面的示例中,我们为线程任务使用了通常的函数。实际上,我们可以使用任何可调用对象或者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函数, 在我们的例子中,没有传递参数。我们本质上可以用 {} 作为一个任务 , 并把它分配给我们的线程。
来源:https://blog.csdn.net/y396397735/article/details/78838236


猜你喜欢
- 一、网站微信扫码支付开发并没有现成的java示例,总结一下自己微信扫码支付心得二、首先去微信公众平台申请账户 https://mp.weix
- JPA是什么? JPA(Java Persistence API)是Sun官方提出的Java持久化规范. 为Java开发人员提供了一种对象/
- 一、使用PinYinConverterCore获取汉语拼音最新在做一个搜索组件,需要使用汉语拼音的首字母查询出符合条件的物品名称,由于汉字存
- 一、前言 网上有许多的多线程断点续传操作,但总是写的很云里雾里,或者
- 使用python和java实现数独游戏,有比较才有收获哦。1、Python版#--coding:utf-8--import ra
- Java String对象使用方法详解先来看一个例子,代码如下: public class Test { public sta
- 我们第三章分析过客户端接入的流程, 这一小节带大家剖析客户端发送数据, Server读取数据的流程:首先温馨提示, 这一小节高度耦合第三章的
- 这篇文章主要介绍了java接口私有方法实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- idea乱码修改bin目录下的idea.exe.vmoptions无效今天在学习Activiti工作流的时候,发现创建bpmn文件总是出现中
- 更正说明:我之前的的标题有点文不对题,我这篇博客的内容明明说的是:java中对象创建的过程,对内存之种底层的东西,我其实提的不太多。所以我原
- 可扩展标记语言(XML)文件是一种标准的文本文件,它使用特定的标记来描述文档的结构以及其他特性。通过将XML转换为PDF,能够便于文件传输及
- 概述本文基于示例的方式解释控制反转,再看控制反转之前,我们先看下常规控制流程,以数据库访问为例示例并没有实际访问数据,而是基于service
- 代理模式:为其他对象提供一种代理以控制某个对象的访问。用在:在某些情况下,一个客户不想或者不能直接访问另一个对象,而代理对象可以在客户端和目
- 一 . 高效加载 BitmapBitMapFactory 提供了四类方法: decodeFile,decodeResource,decode
- 一个简单的照相功能,拍照之后在另一个activit中显示出拍照的图片。首先是布局文件:<?xml version="1.0&
- DebugLZQ在网上搜索相关文件加密的程序,发现给出的基本都是针对“字符创”、“文本”的加密与解密。对视频文件、图片等一般文件的加密解密程
- 这一篇博客我们来使用UGUI实现图片的拖拽功能。说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一
- 背景介绍1,最近有一个大数据量插入的操作入库的业务场景,需要先做一些其他修改操作,然后在执行插入操作,由于插入数据可能会很多,用到多线程去拆
- 前言相信大家对Android悬浮窗应该是很熟悉了,比如说腾讯视频、爱奇艺等APP都有悬浮窗功能。在你打游戏的同时还可以看视频,充分利用屏幕空
- 0.背景简介微软在 .NET 框架中提供了多种实用的线程同步手段,其中包括 monitor 类及 reader-writer锁。