我基於源代碼的編寫監控文件的程序:https://github.com/kvikas/file-monitor-service/blob/master/異步讀取失敗
我的程序使用的boost ::支持ASIO :: stream_descriptor :: async_read_some()從異步讀取inotify的描述http://linux.die.net/man/7/inotify
我的代碼如下:
構造:
void init(){
int fd = inotify_init1(IN_NONBLOCK);
int wd = inotify_add_watch(fd_, "./test.txt", IN_ALL_EVENTS);
stream_.reset(new boost::asio::posix::stream_descriptor(io_service_, fd_)));
}
的異步讀取:
template<typename Monitor_Handler>
void async_monitor(Monitor_Handler handler) {
stream_->async_read_some(boost::asio::buffer(buffer_),
boost::bind(&monitor::handle_monitor<Monitor_Handler>,
shared_from_this(), boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, handler));
}
的處理程序:
template<typename Monitor_Handler>
void handle_monitor(const boost::system::error_code &ec,
std::size_t bytes_transferred, Monitor_Handler handler) {
//process buffer
async_monitor(handler);
}
的錯誤是,在第一handle_monitor是在第一個變化調用幾次(多個事件如MODIFY,Access中打開...)受監控的文件。之後async_read_some方法被再次調用,但我沒有信號了(該handle_monitor不再被調用)
然而,當我試圖重啓inotify的描述,並再次重新進行添加的監視的文件==>它的工作中, handle_monitor被調用來監視這些文件中的新變化。
修改代碼:
template<typename Monitor_Handler>
void handle_monitor(const boost::system::error_code &ec,
std::size_t bytes_transferred, Monitor_Handler handler) {
//process buffer
async_monitor(handler);
init();//for resetting the inotify desciptor
}
難道你們幫我解釋一下這個????我渴望你的答案.....
謝謝您的回答,我重寫FD變量。但這不是問題。我在這個問題上縮小了我的問題的範圍。 http://stackoverflow.com/questions/16397293/the-read-method-on-the-inotify-descriptor-does-not-return你能幫我解釋一下嗎?希望看到你的回答 – khanhhh89 2013-05-07 02:43:40