博客
关于我
c++11特性之std::thread--进阶二
阅读量:561 次
发布时间:2019-03-09

本文共 2401 字,大约阅读时间需要 8 分钟。

C++11 std::thread 之旅:线程传递参数与高级主题

1. 线程传递参数

在编写多线程程序时,传递参数是一个常见且重要的操作。C++11引入了对std::thread的支持,提供了灵活的参数传递方式。

1.1 按值传递

传递一个std::string对象时,默认行为是按值传递。这意味着每个线程都复制了参数,虽然在大多数情况下不会有性能问题,但在处理大量数据时可能会产生不必要的开销。

#include 
#include
#include
void thread_function(std::string s) {
std::cout << "thread function: 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;
}

1.2 引用传递

如果你希望避免按值传递开销,可以传递std::string的引用。然而,传递引用本身并不会带来性能提升,因为std::thread仍然会复制参数。

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

1.3 使用std::refstd::move

为了实现真正的引用传递,可以使用std::ref将参数包装为引用_wrapper_,或者使用std::move将参数移动到新线程中。

std::thread t(&thread_function, std::ref(s));
// 或者
std::thread t(&thread_function, std::move(s));

2. 线程复制

有时候,你可能需要创建多个线程来执行相同的任务。以下示例尝试通过复制一个线程来实现:

#include 
#include
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;
}

2.1 使用std::move修饰

为了修复上述问题,可以使用std::move修饰线程复制操作。

#include 
#include
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;
}

3. 高级线程功能

3.1 获取线程ID

使用std::thread::get_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;
}

3.2 检查硬件并发线程数量

使用std::thread::hardware_concurrency()可以获取系统当前支持的并发线程数量。

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

3.3 Lambda表达式

C++11还支持在std::thread构造时使用lambda表达式。

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

通过以上内容,可以更好地理解和利用std::thread在C++11中的特性和高级功能。

转载地址:http://htupz.baihongyu.com/

你可能感兴趣的文章
mysql
查看>>
MTK Android 如何获取系统权限
查看>>
MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
查看>>
MySQL - ERROR 1406
查看>>
mysql - 视图
查看>>
MySQL - 解读MySQL事务与锁机制
查看>>
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>