boost 1.60.0

先上代码:

 #include <boost/thread.hpp>
#include <iostream> void add(int &i)
{
std::cout<<"in add, befor ++i, i: "<<i<<std::endl;
++i;
std::cout<<"in add, after ++i, i: "<<i<<std::endl;
} int main()
{
int i = ;
std::cout<<"in main, befor add, i: "<<i<<std::endl;
boost::thread thre(add, i);
thre.join();
std::cout<<"in main, after add, i: "<<i<<std::endl;
std::cin.get();
}

结果:

in main, befor add, i:
in add, befor ++i, i:
in add, after ++i, i:
in main, after add, i:

可以看到虽然函数形参是引用方式,但线程并没有改变主函数中的变量。

将第15行代码改为

 boost::thread thre(add, boost::ref(i));

后,输出:

in main, befor add, i:
in add, befor ++i, i:
in add, after ++i, i:
in main, after add, i:

可以推测:thread启动函数时,使用和bind一样的方式进行参数绑定。虽然形参是引用方式,但是如果不使用ref(),结果依然是值传递。

C++11

而在C++ std中,前一种调用方式会引发编译错误。如果确实有这种需求,可以借助bind函数:

 #include <iostream>
#include <thread> void add(int &i) {
std::cout<<"in add, befor ++i, i: "<<i<<std::endl;
++i;
std::cout<<"in add, after ++i, i: "<<i<<std::endl;
} int main()
{
int i = ;
std::cout<<"in main, befor add, i: "<<i<<std::endl;
auto func = std::bind(add, i);
std::thread thre(func); // or std::thread thre(add, std::ref(i));
thre.join();
std::cout<<"in main, after add, i: "<<i<<std::endl;
std::cin.get();
}

boost和std中的thread的引用参数的更多相关文章

  1. thread - 传递引用参数

    当给 thread 的执行函数传递指针参数时,没有任何问题,但是如果想传递引用,按照普通函数的调用方法会遇到编译失败: #include <iostream> #include <t ...

  2. C++11中std::move、std::forward、左右值引用、移动构造函数的测试

    关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就 ...

  3. C++ 11 中的右值引用

    C++ 11 中的右值引用 右值引用的功能 首先,我并不介绍什么是右值引用,而是以一个例子里来介绍一下右值引用的功能: #include <iostream>    #include &l ...

  4. [转载] C++11中的右值引用

    C++11中的右值引用 May 18, 2015 移动构造函数 C++98中的左值和右值 C++11右值引用和移动语义 强制移动语义std::move() 右值引用和右值的关系 完美转发 引用折叠推导 ...

  5. Item 25: 对右值引用使用std::move,对universal引用则使用std::forward

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 右值引用只能绑定那些有资格被move的对象上去.如 ...

  6. 在Activity中使用Thread导致的内存泄漏

    https://github.com/bboyfeiyu/android-tech-frontier/tree/master/issue-7/%E5%9C%A8Activity%E4%B8%AD%E4 ...

  7. Java中的四种引用

    引用定义 实际上,Java中存在四种引用,它们由强到弱依次是:强引用.软引用.弱引用.虚引用.下面我们简单介绍下这四种引用: 强引用(Strong Reference):通常我们通过new来创建一个新 ...

  8. 谈谈 C++ 中的右值引用

    转自:https://liam0205.me/2016/12/11/rvalue-reference-in-Cpp/ 最近在改 XGBoost 的代码.XGBoost 在代码中使用了很多来自 C++1 ...

  9. std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

随机推荐

  1. SIGPIPE了解

    当未连接成功相机网络,在socket请求中 if (_session->prepareSession("192.168.1.1") != ICH_SUCCEED)    // ...

  2. spring-boot启动信息中non-fatal error

    java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport缺少依赖 ...

  3. 兼容cookie和webStorage

    html页面     <!DOCTYPE html> <html lang="en"> <head>     <meta charset= ...

  4. web storage和cookie的区别

    Web Storage的概念和cookie相似,区别是它是为了更大容量存储设计的.Cookie的大小是受限的,并且每次你请求一个新的页面的时候Cookie都会被发送过去,这样无形中浪费了带宽,另外co ...

  5. PC小技巧

    一.如何在office 2010中安装 Microsoft Office Document Imaging 我用的是office 2010版本,如下操作可以把照片转换成文本:第一步:使用Microso ...

  6. 转载C#下RSA算法的实现(适用于支付宝和易宝支付)

    RSA算法代码: using System; using System.Collections.Generic; using System.Text; using System.IO; using S ...

  7. DotnetBar在VS2010工具箱中不显示问题

    请参考:http://blog.csdn.net/yanbo710148546/article/details/7862819

  8. 16101301(MaterialLOD QualitySwitch)

    [目标] MaterialLOD QualitySwitch [思路] 1 QualitySwitch UE4有三挡 UE3 2 现在UE3需要添加三挡 3 UE3 class UMaterialEx ...

  9. 前端神器Sublime Text3 常用插件&常用快捷键

    Sublime Text3常用插件 使用Package Control组件安装 也可以安装package control组件,然后直接在线安装: 按Ctrl+`调出console(注:安装有QQ输入法 ...

  10. PV与并发之间换算的算法换算公式

    并发连接数 = PV / 统计时间 * 页面衍生连接次数 * http响应时间 * 因数 / web服务器数量 PV = 并发连接数 * 统计时间 * web服务器数量/ 页面衍生连接次数 / htt ...