#include <iostream>       // std::cout
#include <functional> // std::ref
#include <thread> // std::thread
#include <future> // std::promise, std::future void print_int1(int& value) {
std::cout << "value1: " << value << '\n'; // 打印 value: 10.
value = ;
} void print_int2(int value) {
std::cout << "value2: " << value << '\n'; // 打印 value: 10.
value = ;
} int main ()
{
int value1 = ;
int value2 = ;
std::thread t1(print_int1, std::ref(value1));
t1.join(); std::thread t2(print_int2, value2);
t2.join();
std::cout << "value1: " << value1 << '\n'; // 打印 value: 10.
std::cout << "value2: " << value2 << '\n'; // 打印 value: 10.
return ;
}

g++ future.cpp  -std=c++1y -g  -pthread

.输出:

value1:
value2:
value1:
value2:

可见,当函数  print_int1(int&),且std::ref(value1) 时,是传递的引用。

#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
++n1; // increments the copy of n1 stored in the function object
++n2; // increments the main()'s n2
// ++n3; // compile error
}
 
int main()
{
int n1 = 1, n2 = 2, n3 = 3;
std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
n1 = 10;
n2 = 11;
n3 = 12;
std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
bound_f();
std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << '\n';
}

输出:

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

http://zh.cppreference.com/w/cpp/utility/functional/ref

#include <thread>
#include <iostream>
using namespace std; void func1(int& a)
{
a++;
} void func2(int a)
{
a++;
} int main()
{
int a = ;
std::thread t1(func1, std::ref(a));
std::thread t2(func2, a);
std::thread t3(func2, std::ref(a));
//std::thread t4(func1, a); 编译错误
t1.join();
t2.join();
t3.join(); std::cout << a << std::endl; return ;
}

输出43,注意,func1 的参数是引用,因此要用std::ref,  func2的参数是值拷贝,因此传a,对于 thread3,虽然传的是std::ref, 但由于参数是值拷贝,因此不会对a有影响。

对于 thread4,会产生编译错误,还在思考为什么thread3 不会产生编译错误

理解了bind也就好理解上面的thread3 是可以编译通过的原因,

http://www.cnblogs.com/xusd-null/p/3698969.html

(原文:http://blog.think-async.com/2010/04/bind-illustrated.html)

本文解释了bind 是如何工作的。为了清晰,我对图中的语法作了一些简化(例如,省略函数调用操作符的参数类型),并且简化了 bind 的实现.

1. bind 可以用来将用户提供的需要一个参数的函数转换成不需要参数的函数对象。绑定的值(在这个例子中是123)存储在函数对象内并且会被自动传递给用户指定的函数:

2. 参数绑定也可以用于将类成员函数转换成零参数的函数对象。猿类们都知道,非静态成员函数需要一个隐式的 this 参数。这意味着需要绑定一个合适的类实例指针到这个函数对象:

3. 相应地,隐式的 this 指针也可以显式地传递给需要一个参数的函数对象:

4. 函数对象经常同时使用提前绑定的参数和调用时才提供的参数。这个可以用成员函数来实现:

5. 当然也可以使用非成员函数:

6. 有些时候函数对象被调用时会提供多余的参数,而这些参数是目标函数不需要的。bind 会自动忽略这些多余的参数:

7. 这些多余的参数不需要一定在函数对象签名的最后:

8. 最后, bind 还允许重新组织函数对象的参数顺序:

 http://www.cnblogs.com/diegodu/p/6496607.html
http://blog.csdn.net/fcryuuhou/article/details/8568194

最新文章

  1. Andriod学习笔记3:Mac 平台下搭建 CLion 集成开发环境
  2. DotNet程序集解析
  3. 返回数据方法DeaCacheCommand,由CRL自动实现
  4. Used Query
  5. codeforces problem 140E New Year Garland
  6. 进程和线程及Linux下的编程
  7. SSDP
  8. 通过Hander进行界面刷新
  9. Codeforces Round #363 Fix a Tree(树 拓扑排序)
  10. [Mysql] MySQL配置文件my.cnf的理解
  11. 守望先锋overwatch美服外服设置方法
  12. Git 上传本地命令
  13. NodeJS爬虫系统初探
  14. poj2104 Kth-Number
  15. web项目jsp出现The superclass javax.servlet.http.HttpServlet was not found on the Java Build Path错误
  16. 圆方树简介(UOJ30:CF Round #278 Tourists)
  17. Java 反射 Method threw &#39;java.lang.InstantiationException&#39; exception.
  18. CAS server打包小白教程
  19. Oracle 数据文件迁移
  20. 关于JDBC和连接池我学到的(转载保存)

热门文章

  1. cpu亲和性绑定
  2. 哈尔滨理工大学第七届程序设计竞赛(G.Great Atm)
  3. Extjs MVC模式
  4. [CF340D]Bubble Sort Graph/[JZOJ3485]独立集
  5. [转]Java transient关键字
  6. 星际争霸 虚空之遗 人族5BB 操作流程
  7. 阿里云ECS使用SSH连接CentOS 6.9经常断线的问题解决:OperationTimedOut
  8. opengl笔记——OpenGL好资料备忘
  9. QS世界大学排名_百度百科
  10. Word中将图表变为表格