// 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;
}