Boost_Asio(2) bind

// asio_bind.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <boost/bind.hpp>
#include <iostream>
void Func()
{
	std::cout << "func has been called!" << std::endl;
}

void Func2(int i, float f)
{
	std::cout << "i:" << i << std::endl;
	std::cout << "f:" << f << std::endl;
}

class MyClass
{
public:
	void Func3(int i, float f)
	{
		std::cout << "i: " << i << std::endl;
		std::cout << "f: " << f << std::endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	///< 因为我们创建一个函数触发对象,但是没有实际调用。我们需要使用()操作符调用函数
	boost::bind(&Func)();
	///< 向调用的函数传参数
	boost::bind(&Func2, 1, 3.14f)();
	///< 调用类成员函数
	///< 我们必须传递类对象的地址以便调用。如果是在类内部调用,则调用this指针或者shared_from_this().
	MyClass c;
	boost::bind(&MyClass::Func3, &c, 13, 13.24)();

	system("pause");
	return 0;
}
// Bind_in_thread.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#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";
}
/*
	在多线程中,io_service作为全局对象。在实际应用中,这种做法是不推荐的。
	如果我们尝试应用bind io_service对象,则会发生错误,因为io_service不能被拷贝,
	所以我们需要使用shared_ptr.
*/
int main(int argc, char * argv[])
{
	/*
		boost::shared_ptr是可以共享所有权的指针。
		如果有多个shared_ptr共同管理同一个对象时,只有这些shared_ptr全部与该对象脱离关系之后,被管理的对象才会被释放。
	*/
	boost::shared_ptr< boost::asio::io_service > io_service(///< 此指针指向io_service - 2018/05/14
		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();

	system("pause");
	return 0;
}
// bind_thread_mutex.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
#include <iostream>
///< 异步程序中,需要确认全局和共享数据的同步访问。下列示例示范了mutex对象的使用方法
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();

	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值