当给 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 - 传递引用参数的更多相关文章

  1. java方法强制传递引用参数(做为返回值),改变被传递参数值。

    Java传递参数分为2种: 值类型,Java里面也叫简单类型,这种参数类型的传递的是它的副本拷贝: 引用类型,传递的是对象引用地址,如果在方法内改变该参数对象属性即是对原引用对象的改变:如果不想这样传 ...

  2. boost和std中的thread的引用参数

    boost 1.60.0 先上代码: #include <boost/thread.hpp> #include <iostream> void add(int &i) ...

  3. 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用

    原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...

  4. Java 给Thread传递参数

    一开始我想把run()函数写成有参函数来传值,后来发现行不通.经过查找,最终用如下方法传递了参数: 也就是用另外一个有参函数setTar()传递参数. 调用的时候用这4行code传递参数: 上面是用i ...

  5. Web报表页面如何传递中文参数

    1.场景描述 在用报表开发工具FineReport设计的web报表中,给iframe设置src嵌入某个报表时,往往会给报表传递初始的参数值,例如: <iframe id="report ...

  6. 传递引用类型参数的两种方式(转自 MSDN)

    引用类型的变量不直接包含其数据:它包含的是对其数据的引用.当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值(更改属性的值),但是无法更改引用本身的值:也就是说,不能使用相同的引 ...

  7. 传递引用类型参数(ref)

    引用类型的变量不直接包含其数据:它包含的是对其数据的引用. 当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值. 但是无法更改引用本身的值:也就是说,不能使用相同的引用为新类分配 ...

  8. 实例对比剖析c#引用参数的用法

    c#引用参数传递的深入剖析值类型的变量存储数据,而引用类型的变量存储对实际数据的引用.(这一点很重要,明白了之后就能区分开值类型和引用类型的差别) 在参数传递时,值类型是以值的形式传递的(传递的是值, ...

  9. C#方法的六种参数,值参数、引用参数、输出参数、参数数组、命名参数、可选参数

    方法的参数有六种,分别是值参数.引用参数.输出参数.参数数组.命名参数.可选参数. 值参数 值参数是方法的默认类型,通过复制实参的值到形参的方式把数据传递到方法,方法被调用时,系统作两步操作: 在栈中 ...

随机推荐

  1. 洛谷 P1088 火星人

    https://www.luogu.org/problemnew/show/P1088 这个题一开始是很蒙的 感觉很麻烦,每次都要交换balabala..... 后来才知道有这么一个神奇的stl 真是 ...

  2. linux添加超级管理员用户,修改,删除用户

    useradd一个用户后,去修改/etc/passwd文件中的这个用户这一行,把其中的uid改为0,gid改为0(其中****代表一个用户名)这样****就具有root权限了 如:root2:x:0: ...

  3. 雷军微博拧螺丝,CFO为粉丝数发愁

    导读 小米集团联合创始人.品牌战略官黎万强4年前写的书<参与感>,估计又要热卖了. 3月24日,小米CFO周受资发微博,“我刚接受了同事的挑战,要在一定时间内在微博上有更多的粉丝”,并向粉 ...

  4. Django_rbac_demo 权限控制组件框架模型

    rbac 权限控制组件 基于角色的权限控制 本质每个权限即为一个 URL 项目组件结构 表结构 Role (title, permission) -(ManyToManyField)-   User  ...

  5. 【并发编程】【JDK源码】CAS与synchronized

    线程安全 众所周知,Java是多线程的.但是,Java对多线程的支持其实是一把双刃剑.一旦涉及到多个线程操作共享资源的情况时,处理不好就可能产生线程安全问题.线程安全性可能是非常复杂的,在没有充足的同 ...

  6. JMeter5.1开发TCP协议接口脚本

    最简单的方法,就是找开发给报文,直接复制到tcp取样器中,将需要变化的值做参数化就可以了.(xml报文要去掉回车换行) 下面是一个通讯头定义 通讯头56个字节(1个字符一个字节) 3 + 9 + 9 ...

  7. 机器学习之正则化【L1 & L2】

    前言 L1.L2在机器学习方向有两种含义:一是L1范数.L2范数的损失函数,二是L1.L2正则化 L1范数.L2范数损失函数 L1范数损失函数: L2范数损失函数: L1.L2分别对应损失函数中的绝对 ...

  8. CopyOnWriteArrayList真的完全线程安全吗

    我之前书上看到的说法是:Vector是相对线程安全,CopyOnWriteArrayList是绝对线程安全 这种说法其实有些问题,CopyOnWriteArrayList在某些场景下还是会报错的 Co ...

  9. NOI-OJ 2.2 ID:6261 汉诺塔

    思路 汉诺塔是递归思想最经典的例子,通过递归不断缩小问题,将n个盘子的问题简化n-1个,直至1个. 三个盘子,分别为A:from,B:to,C:by(A为起点盘,B为目标盘,C为中转盘) 过程 将1- ...

  10. smartgit

    1.同步最新分支 2.smartgit ctrl+2 可以看到本地新增加的文件