thread - 传递引用参数
当给 thread 的执行函数传递指针参数时,没有任何问题,但是如果想传递引用,按照普通函数的调用方法会遇到编译失败:
#include <iostream>
#include <thread>
#include <string> int main()
{
std::string a("Hello");
std::cout << "address = " << &a << "\n";
std::thread t([](std::string& a)
{
std::cout << "in thread address = " << &a << "\n";
}, a);
t.join();
}
编译:g++ -std=c++11 -pthread test.cpp
失败了:
In file included from /usr/include/c++/4.7/bits/move.h::,
from /usr/include/c++/4.7/bits/stl_pair.h:,
from /usr/include/c++/4.7/bits/stl_algobase.h:,
from /usr/include/c++/4.7/bits/char_traits.h:,
from /usr/include/c++/4.7/ios:,
from /usr/include/c++/4.7/ostream:,
from /usr/include/c++/4.7/iostream:,
from test.cpp::
/usr/include/c++/4.7/type_traits: In instantiation of ?.truct std::_Result_of_impl<false, false, main()::<lambda(std::string&)>, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >?.
/usr/include/c++/4.7/type_traits::: required from ?.lass std::result_of<main()::<lambda(std::string&)>(std::basic_string<char>)>?
/usr/include/c++/4.7/functional::: required from ?.truct std::_Bind_simple<main()::<lambda(std::string&)>(std::basic_string<char>)>?
/usr/include/c++/4.7/thread::: required from ?.td::thread::thread(_Callable&&, _Args&& ...) [with _Callable = main()::<lambda(std::string&)>; _Args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]?
test.cpp::: required from here
/usr/include/c++/4.7/type_traits::: error: no match for call to ?.main()::<lambda(std::string&)>) (std::basic_string<char>)?
test.cpp::: note: candidates are:
In file included from /usr/include/c++/4.7/bits/move.h::,
from /usr/include/c++/4.7/bits/stl_pair.h:,
from /usr/include/c++/4.7/bits/stl_algobase.h:,
from /usr/include/c++/4.7/bits/char_traits.h:,
from /usr/include/c++/4.7/ios:,
from /usr/include/c++/4.7/ostream:,
from /usr/include/c++/4.7/iostream:,
from test.cpp::
/usr/include/c++/4.7/type_traits::: note: void (*)(std::string&) {aka void (*)(std::basic_string<char>&)} <conversion>
/usr/include/c++/4.7/type_traits::: note: candidate expects arguments, provided
test.cpp::: note: main()::<lambda(std::string&)>
test.cpp::: note: no known conversion for argument from ?.td::basic_string<char>?.to ?.td::string& {aka std::basic_string<char>&}?
这里类似于 std::bind,std::thread 和 std::bind 采用了相同的机制,必须使用 std::ref 来包装:
#include <iostream>
#include <thread>
#include <string> int main()
{
std::string a("Hello");
std::cout << "address = " << &a << "\n";
std::thread t([](std::string& a)
{
std::cout << "in thread address = " << &a << "\n";
}, std::ref(a));
t.join();
}
thread - 传递引用参数的更多相关文章
- java方法强制传递引用参数(做为返回值),改变被传递参数值。
Java传递参数分为2种: 值类型,Java里面也叫简单类型,这种参数类型的传递的是它的副本拷贝: 引用类型,传递的是对象引用地址,如果在方法内改变该参数对象属性即是对原引用对象的改变:如果不想这样传 ...
- boost和std中的thread的引用参数
boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...
- 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用
原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...
- Java 给Thread传递参数
一开始我想把run()函数写成有参函数来传值,后来发现行不通.经过查找,最终用如下方法传递了参数: 也就是用另外一个有参函数setTar()传递参数. 调用的时候用这4行code传递参数: 上面是用i ...
- Web报表页面如何传递中文参数
1.场景描述 在用报表开发工具FineReport设计的web报表中,给iframe设置src嵌入某个报表时,往往会给报表传递初始的参数值,例如: <iframe id="report ...
- 传递引用类型参数的两种方式(转自 MSDN)
引用类型的变量不直接包含其数据:它包含的是对其数据的引用.当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值(更改属性的值),但是无法更改引用本身的值:也就是说,不能使用相同的引 ...
- 传递引用类型参数(ref)
引用类型的变量不直接包含其数据:它包含的是对其数据的引用. 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值. 但是无法更改引用本身的值:也就是说,不能使用相同的引用为新类分配 ...
- 实例对比剖析c#引用参数的用法
c#引用参数传递的深入剖析值类型的变量存储数据,而引用类型的变量存储对实际数据的引用.(这一点很重要,明白了之后就能区分开值类型和引用类型的差别) 在参数传递时,值类型是以值的形式传递的(传递的是值, ...
- C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数
方法的参数有六种,分别是值参数.引用参数.输出参数.参数数组.命名参数.可选参数. 值参数 值参数是方法的默认类型,通过复制实参的值到形参的方式把数据传递到方法,方法被调用时,系统作两步操作: 在栈中 ...
随机推荐
- AssetBundle打包
为热更新打基础(xlua\tolua) 素材.源码链接:http://www.sikiedu.com/course/74/task/1812/show 一.AssetBundle的定义和作用 1,As ...
- Zabbix配置邮件监控
zabbix服务端配置 安装软件并配置 使用第三方邮件实现报警 1. 安装软件 $ yum -y install mailx 2. 配置发送邮件账号密码和服务器 $ vim /etc/mail.rc ...
- MT【318】分式不等式双代换
已知$a,b>0$且$\dfrac{1}{a}+\dfrac{1}{b}=\dfrac{2}{3}$,求$\dfrac{1}{a-1}+\dfrac{4}{b-1}$的最小值. 解:令$m=\d ...
- 【nginx】nginx日常命令
看下nginx命令的帮助信息 [root@localhost oa_workflow_test]# /usr/local/nginx/sbin/nginx -h nginx version: ngin ...
- [FJOI2018]领导集团问题
[FJOI2018]领导集团问题 dp[i][j],i为根子树,最上面的值是j,选择的最大值 观察dp方程 1.整体Dp已经可以做了. 2.考虑优美一些的做法: dp[i]如果对j取后缀最大值,显然是 ...
- bzoj4170 极光
题目链接 题面 题意 把每个位置的点都看成是一个二维坐标系中的点.比如第\(i\)个点就是\((i,a[i])\). 有两种操作 询问:然后每次询问的就是与当前点坐标的曼哈顿距离小于等于\(k\)的点 ...
- Day044--javascript, ECMAScript
一. javascript JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:操作网页上的 ...
- 三步解决fiddler升级后https无法通过证书验证问题
有时候使用fiddler时,https页面会出现错误提示,我们可以这样设置来避免错误 第一步:去掉https的抓取 Tools>Option 去掉Capture HTTPS CONNECTs 的 ...
- linux系统无法启动或无法登入
修改root权限: https://blog.csdn.net/houjue2298/article/details/78539827 修改密码: https://www.cnblogs.com/we ...
- Kubernetes之RBAC
API Server的授权管理 API Server 内部通过用户认证后,然后进入授权流程.对合法用户进行授权并且随后在用户访问时进行鉴权,是权限管理的重要环节.API Server 目前支持一下几种 ...