int main() {
talk_to_client::ptr client = talk_to_client::new_();
acc.async_accept(client->sock(), boost::bind(handle_
accept,client,_1));
service.run();
}
boost::thread_group threads;
void listen_thread() {
service.run();
}
void start_listen(int thread_count) {
for ( int i = 0; i < thread_count; ++i)
threads.create_thread( listen_thread);
}
int main(int argc, char* argv[]) {
talk_to_client::ptr client = talk_to_client::new_();
acc.async_accept(client->sock(), boost::bind(handle_
accept,client,_1));
start_listen(100);
threads.join_all();
}
void do_read() {
async_read(sock_, buffer(read_buffer_),MEM_FN2(read_complete,_1,_2), MEM_FN2(on_read,_1,_2));
post_check_ping();
}
void post_check_ping() {
timer_.expires_from_now(boost::posix_time::millisec(5000));
timer_.async_wait( MEM_FN(on_check_ping));
}
void update_clients_changed() {
array copy;
{ boost::recursive_mutex::scoped_lock lk(clients_cs);
copy = clients; }
for( array::iterator b = copy.begin(), e = copy.end(); b != e;
++b)
(*b)->set_clients_changed();
}
void my_func() {
...
}
service.post(my_func);
void on_complete() {
...
}
void my_func() {
...
service.post(on_complete);
}
async_call(my_func);
struct async_op : boost::enable_shared_from_this<async_op>, ... {
typedef boost::function<void(boost::system::error_code)>
completion_func;
typedef boost::function<boost::system::error_code ()> op_func;
struct operation { ... };
void start() {
{ boost::recursive_mutex::scoped_lock lk(cs_);
if ( started_) return; started_ = true; }
boost::thread t( boost::bind(&async_op::run,this));
}
void add(op_func op, completion_func completion, io_service
&service) {
self_ = shared_from_this();
boost::recursive_mutex::scoped_lock lk(cs_);
ops_.push_back( operation(service, op, completion));
if ( !started_) start();
}
void stop() {
boost::recursive_mutex::scoped_lock lk(cs_);
started_ = false; ops_.clear();
} private:
boost::recursive_mutex cs_;
std::vector<operation> ops_; bool started_; ptr self_;
};
struct async_op : boost::enable_shared_from_this<async_op>
, private boost::noncopyable {
struct operation {
operation(io_service & service, op_func op, completion_
func completion)
: service(&service), op(op), completion(completion)
, work(new io_service::work(service))
{}
operation() : service(0) {}
io_service * service;
op_func op;
completion_func completion;
typedef boost::shared_ptr<io_service::work> work_ptr;
work_ptr work;
};
... };
struct async_op : ... {
typedef boost::shared_ptr<async_op> ptr;
static ptr new_() { return ptr(new async_op); }
...
void run() {
while ( true) {
{ boost::recursive_mutex::scoped_lock lk(cs_);
if ( !started_) break; }
boost::this_thread::sleep( boost::posix_
time::millisec(10));
operation cur;
));
}
{ boost::recursive_mutex::scoped_lock lk(cs_);
if ( !ops_.empty()) {
cur = ops_[0]; ops_.erase( ops_.begin());
}}
if ( cur.service)
cur.service->post(boost::bind(cur.completion, cur.op()
self_.reset();
}
};
size_t checksum = 0;
boost::system::error_code compute_file_checksum(std::string file_name)
{
HANDLE file = ::CreateFile(file_name.c_str(), GENERIC_READ, 0, 0,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0);
windows::random_access_handle h(service, file);
long buff[1024];
checksum = 0;
size_t bytes = 0, at = 0;
boost::system::error_code ec;
while ( (bytes = read_at(h, at, buffer(buff), ec)) > 0) {
at += bytes; bytes /= sizeof(long);
for ( size_t i = 0; i < bytes; ++i)
checksum += buff[i];
}
return boost::system::error_code(0, boost::system::generic_
category());
}
void on_checksum(std::string file_name, boost::system::error_code) {
std::cout << "checksum for " << file_name << "=" << checksum <<
std::endl;
}
int main(int argc, char* argv[]) {
std::string fn = "readme.txt";
async_op::new_()->add( service, boost::bind(compute_file_
checksum,fn),
boost::bind(on_checksum,fn,_1));
service.run();
}
原文地址:http://blog.csdn.net/mmoaay/article/details/41008049