asio 中的io_service::work

Class to inform the io_service when it has work to do.

class work
Member Functions

Name

Description

get_io_service

Get the io_service associated with the work.

io_service

(Deprecated: use get_io_service().) Get the io_service associated with the work.

work

Constructor notifies the io_service that work is starting.

~work

Destructor notifies the io_service that the work is complete.

The work class is used to inform the io_service when work starts and finishes. This ensures that the io_service's run() function will not exit while work is underway, and that it does exit when there is no unfinished work remaining.

The work class is copy-constructible so that it may be used as a data member in a handler class. It is not assignable.

 

有些应用程序希望在没有pending的消息时,io.run也不退出.

boost::asio::io_service io_service;

boost::asio::io_service::work work(io_service);

Thread()

{

Io_service.run();

}

如果work不被析构,该线程永远不会退出.在work不被析构得情况下就让其退出,可以调用io.stop。这将导致 io.run立刻退出,所有未完成的消息都将丢弃。已完成的消息(但尚未进入handler的)也不会调用其handler函数(由于在stop中设置了 stopped_= 1).

work提供了一个拷贝构造函数,因此可以直接在任意地方使用。对于一个io_service来说,有多少个work实例关 联,则outstanding_work_就+1了多少次,只有关联到同一个io_service的work全被析构之后,io.run才会在所有消息处 理结束之后正常退出。

 

 

 

posted @ 2012-03-07 23:22  曙光中睡懒觉  阅读(5761)  评论(0编辑  收藏  举报