boost::bind会返回一个函数对象,它内部保存了数据的拷贝
2016-10-03 22:54
381 查看
boost::bind会返回一个函数对象,它内部保存了数据的拷贝。
其结果如下所示:
====== not use reference ======
main , 2016-10-03T22:54:46, str=before modify
other, 2016-10-03T22:54:46, msg=before modify
main , 2016-10-03T22:54:49, str=after modify
other, 2016-10-03T22:54:52, msg=before modify
====== use reference ======
main , 2016-10-03T22:55:04, str=before modify
other, 2016-10-03T22:55:04, msg=before modify
main , 2016-10-03T22:55:07, str=after modify
other, 2016-10-03T22:55:10, msg=after modify
完。
#include <string> #include <iostream> #include <thread> #include <boost/thread.hpp> #include <boost/asio.hpp> std::string LocalTime() { time_t tt = std::chrono::system_clock::to_time_t (std::chrono::system_clock::now()); struct tm* ptm = localtime(&tt); std::string buff(20, '\0');//[19个长度]+[结尾符'\0']=[20个长度], std::sprintf(const_cast<char*>(buff.c_str()), "%04d-%02d-%02dT%02d:%02d:%02d", (int)ptm->tm_year + 1900, (int)ptm->tm_mon + 1, (int)ptm->tm_mday, (int)ptm->tm_hour, (int)ptm->tm_min, (int)ptm->tm_sec); buff.resize(19); return buff; } void TestFun(const std::string& msg) { if (true) { std::cout << "other, " << LocalTime() << ", msg=" << msg << std::endl; } std::this_thread::sleep_for(std::chrono::seconds(6)); if (true) { std::cout << "other, " << LocalTime() << ", msg=" << msg << std::endl; } } int main() { //《Boost程序库完全开发指南 第3版》P454: 绑定完成后,bind会返回一个函数对象,它内部保存了f的拷贝,... boost::thread_group thgp; boost::asio::io_service io; boost::asio::io_service::work wk(io); boost::system::error_code ec; thgp.create_thread(boost::bind(&boost::asio::io_service::run, boost::ref(io), ec)); if (true) { std::cout << "====== not use reference ======" << std::endl; std::string str; if (true) { str = "before modify"; std::cout << "main , " << LocalTime() << ", str=" << str << std::endl; } //参数str没有传递引用,此时bind会保存str的拷贝, io.post(boost::bind(TestFun, str )); //io.post(boost::bind(TestFun, boost::ref(str))); std::this_thread::sleep_for(std::chrono::seconds(3)); if (true) { str = "after modify"; std::cout << "main , " << LocalTime() << ", str=" << str << std::endl; } std::this_thread::sleep_for(std::chrono::seconds(15)); } if (true) { std::cout << "====== use reference ======" << std::endl; std::string str; if (true) { str = "before modify"; std::cout << "main , " << LocalTime() << ", str=" << str << std::endl; } //参数str传递了引用,此时bind会保存str的引用,如果str被改变了,那么函数对象里面的str自然被改变了, //io.post(boost::bind(TestFun, str )); io.post(boost::bind(TestFun, boost::ref(str))); std::this_thread::sleep_for(std::chrono::seconds(3)); if (true) { str = "after modify"; std::cout << "main , " << LocalTime() << ", str=" << str << std::endl; } std::this_thread::sleep_for(std::chrono::seconds(15)); } return 0; }
其结果如下所示:
====== not use reference ======
main , 2016-10-03T22:54:46, str=before modify
other, 2016-10-03T22:54:46, msg=before modify
main , 2016-10-03T22:54:49, str=after modify
other, 2016-10-03T22:54:52, msg=before modify
====== use reference ======
main , 2016-10-03T22:55:04, str=before modify
other, 2016-10-03T22:55:04, msg=before modify
main , 2016-10-03T22:55:07, str=after modify
other, 2016-10-03T22:55:10, msg=after modify
完。
相关文章推荐
- 接受int形参(double,,等等)返回int(...);并且一个vector对象保存指向这些函数的指针;进行加减乘除;输出结果
- 一个关于函数内部指针参数返回的错误调试及分析
- javascrip setInterval 函数在一个对象内部的调用问题
- 函数返回一个对象时要调用复制构造函数的原因
- 在ajax循环加载多条数据,并在内部定义一个函数
- cpp,函数返回一个局部对象。
- Effective C++ Item 28 避免返回对象内部数据的引用或指针
- 【C++】boost::bind和函数对象一起使用实现便捷的异步编程
- 函数返回一个SqlDataReader对象
- C++函数返回一个对象学习笔记
- Netty 的Channel write返回一个ChannelFuture 对象内部实现
- 关于函数的返回值是一个接口类型的对象
- 【php数组函数序列】之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
- Boost.Bind用法详解(一) 2008-05-09 15:50:50| 分类: C++ |字号 订阅 Boost.Bind 为函数和函数对象提供了一致的语法,对于值语义和指针语义也一样。
- 根据指定方法的参数去构造一个新的对象的拷贝并将他返回
- 条款31: 千万不要返回局部对象的引用,也不要返回函数内部用new初始化的指针的引用 (转自effective c++ second edition)
- 函数对象function object 以及boost::bind的一点了解
- PHP 为你提供一个叫__toString() 的函数,你可以用它来返回表示对象的字符串信息,而且一旦定义它,打印命令将调用它并打印出返回的字符串。
- 条款31: 千万不要返回局部对象的引用,也不要返回函数内部用new初始化的指针的引用
- Ajax使用Action中返回的Json数据的完整实例(二):action内将一个实体对象转化为json并返回