boost asio 学习(二)了解boost::bind
2.了解boost::bind
使用boost::bind封装一个函数,考虑以下例子
示例2a
- #include <iostream>
- #include <boost/bind.hpp>
- void F1()
- {
- std::cout << __FUNCTION__ << std::endl;
- }
- int main( int argc, char * argv[] )
- {
- boost::bind( &F1 );
- return 0;
- }
运行代码无输出,这是因为我们创建一个函数触发对象,但是没有实际调用。我们需要使用()操作符调用函数.
示例2b
- #include <iostream>
- #include <boost/bind.hpp>
- void F1()
- {
- std::cout << __FUNCTION__ << std::endl;
- }
- int main( int argc, char * argv[] )
- {
- boost::bind( &F1 )();
- return 0;
- }
现在运行示例,将输出文本。下面示例介绍如何传输参数
示例2C
- #include <iostream>
- #include <boost/bind.hpp>
- void F2( int i, float f )
- {
- std::cout << "i: " << i << std::endl;
- std::cout << "f: " << f << std::endl;
- }
- int main( int argc, char * argv[] )
- {
- boost::bind( &F2, 42, 3.14f )();
- return 0;
- }
运行程序将输出预期的文本。
下个示例显示bind类成员函数
示例2d
- #include <iostream>
- #include <boost/bind.hpp>
- class MyClass
- {
- public:
- void F3( int i, float f )
- {
- std::cout << "i: " << i << std::endl;
- std::cout << "f: " << f << std::endl;
- }
- };
- int main( int argc, char * argv[] )
- {
- MyClass c;
- boost::bind( &MyClass::F3, &c, 42, 3.14f )();
- return 0;
- }
我们必须传递类对象的地址以便调用。如果是在类内部调用,则调用this指针或者shared_from_this().
在多线程中,io_service作为全局对象。在实际应用中,这种做法是不推荐的。如果我们尝试应用bind io_service对象,则会发生错误,因为io_service不能被拷贝,所以我们需要使用
shred_ptr.
示例2e
- #include <boost/asio.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <iostream>
- void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
- {
- std::cout << "Thread Start\n";
- io_service->run();
- std::cout << "Thread Finish\n";
- }
- int main( int argc, char * argv[] )
- {
- boost::shared_ptr< boost::asio::io_service > io_service(
- new boost::asio::io_service
- );
- boost::shared_ptr< boost::asio::io_service::work > work(
- new boost::asio::io_service::work( *io_service )
- );
- std::cout << "Press [return] to exit." << std::endl;
- boost::thread_group worker_threads;
- for( int x = 0; x < 4; ++x )
- {
- worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
- }
- std::cin.get();
- io_service->stop();
- worker_threads.join_all();
- return 0;
- }
异步程序中,需要确认全局和共享数据的同步访问。下列示例示范了mutex对象的使用方法。
示例2f
- #include <boost/asio.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/thread.hpp>
- #include <boost/thread/mutex.hpp>
- #include <boost/bind.hpp>
- #include <iostream>
- boost::mutex global_stream_lock;
- void WorkerThread( boost::shared_ptr< boost::asio::io_service > io_service )
- {
- global_stream_lock.lock();
- std::cout << "[" << boost::this_thread::get_id() <<
- "] Thread Start" << std::endl;
- global_stream_lock.unlock();
- io_service->run();
- global_stream_lock.lock();
- std::cout << "[" << boost::this_thread::get_id() <<
- "] Thread Finish" << std::endl;
- global_stream_lock.unlock();
- }
- int main( int argc, char * argv[] )
- {
- boost::shared_ptr< boost::asio::io_service > io_service(
- new boost::asio::io_service
- );
- boost::shared_ptr< boost::asio::io_service::work > work(
- new boost::asio::io_service::work( *io_service )
- );
- global_stream_lock.lock();
- std::cout << "[" << boost::this_thread::get_id()
- << "] Press [return] to exit." << std::endl;
- global_stream_lock.unlock();
- boost::thread_group worker_threads;
- for( int x = 0; x < 4; ++x )
- {
- worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
- }
- std::cin.get();
- io_service->stop();
- worker_threads.join_all();
- return 0;
- }
此类mutex对象不可递归锁定。如果递归锁定将造成死锁。
boost asio 学习(二)了解boost::bind的更多相关文章
- boost asio 学习(九) boost::asio 网络封装
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=10 9. A ...
- boost asio 学习(八) 网络基础 二进制写发送和接收
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=9 8. Net ...
- boost asio 学习(七) 网络基础 连接器和接收器(TCP示例)
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=8 7. Net ...
- boost asio 学习(六) 定时器
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting- started-with-boostasio?pg=7 6 定时器 ...
- BOOST ASIO 学习专贴
本文已于20170903更新完毕,所有boost asio 代码均为本人手抄.编译器为vs2013,并且所有代码已经上传,本文下方可下载源码 为了学习boost asio库,我是从boost的官方bo ...
- boost::asio 学习
安装 下载-解压 指定安装目录 ./bootstrap.sh --prefix=/usr/local/boost_1_68_0 查看所有必须要编译才能使用的库 ./b2 --show-librarie ...
- boost asio 学习(五) 错误处理
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=6 5. Erro ...
- boost asio 学习(四)使用strand将任务排序
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=5 4. Seri ...
- boost asio 学习(三)post与dispatch
http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4 本章节为io_ ...
随机推荐
- VUE 微信开发
1.工具 1.电脑版微信客户端window版本(1.x.x 亲测可以在谷歌浏览器进行微信授权登录,版本越来越好)或者用微信开发工具.很久之前就是用这个方法搞定用chrome进行微信登录授权. 2.us ...
- python调用linux的命令
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- 解决双击excel文件打开多个excel.exe进程的问题
解决双击excel文件打开多个excel.exe进程的问题有些时候,双击两个excel文件,会打开多个excel进程,不同进程之间不能复制粘贴公式,只能粘贴数值,很不方便.怎么样双击多个excel文件 ...
- 微信小程序如何设置服务器配置
最近微信小程序在it界火了起来,公司也要求我们开始接触微信小程序,废话不多说直接从配置微信小程序开始 1,首先,登录 https://mp.weixin.qq.com,(这里默认你已经获取到微信小程序 ...
- JavaWeb——tomcat manager 403 Access Denied .You are not authorized to view this page.
403 Access Denied You are not authorized to view this page. If you have already configured the Manag ...
- 【亲测显式等待】Selenium:元素等待的4种方法
Selenium:元素等待的4种方法 1.使用Thread.sleep(),这是最笨的方法,但有时候也能用到而且很实用. 2.隐式等待,隐性等待是指当要查找元素,而这个元素没有马上出现时,告诉We ...
- nginx ------反向代理和负载均衡
最近由于公司的业务增长 服务器承受不住压力经常出现崩溃现象 为了解决 使用nginx的负载均衡解决,以下是操作步骤: 1.nginx 的负载均衡:将压力分散到不同的机器上 nginx不单可以作为强大的 ...
- 分享下自己写的一个微信小程序请求远程数据加载到页面的代码
1 思路整理 就是页面加载完毕的时候 请求远程接口,然后把数据赋值给页面的变量 ,然后列表循环 2 js相关代码 我是改的 onload函数 /** * 生命周期函数--监听页面加载 */ on ...
- 一份快速完整的Tensorflow模型保存和恢复教程(译)(转载)
该文章转自https://blog.csdn.net/sinat_34474705/article/details/78995196 我在进行图像识别使用ckpt文件预测的时候,这个文章给我提供了极大 ...
- list按照某个元素进行排序
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.u ...