Class to inform the io_service when it has work to do.
class work
Member Functions
Name |
Description |
---|---|
Get the io_service associated with the work. |
|
(Deprecated: use get_io_service().) Get the io_service associated with the work. |
|
Constructor notifies the io_service that work is starting. |
|
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才會在所有消息處 理結束之后正常退出。