我尝试在c++中在线程中使用inotify但是select是阻塞的,所以当我的应用程序退出时,我永远不能离开线程
如何创建inotify watch
fd=inotify_init1(IN_NONBLOCK);
// checking for error
if ( fd < 0 )
log->Print("Could not init files listener");
else
{
// use select watch list for non-blocking inotify read
FD_ZERO( &watch_set );
FD_SET( fd, &watch_set );
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
// watch directory for any activity and report it back to me
int wd=inotify_add_watch(fd,folder.c_str(),IN_ALL_EVENTS);
// add wd and directory name to Watch map
watch.insert( -1, folder, wd );
// start listening thread
run(FilesListener::threadBootstrap);
}
这是在我的线程循环中调用的函数
void FilesListener::refresh()
{
char buffer[1024];
// select waits until inotify has 1 or more events.
// select needs the highest fd (+1) as the first parameter.
select( fd+1, &watch_set, NULL, NULL, NULL );
// Read event(s) from non-blocking inotify fd (non-blocking specified in inotify_init1 above).
int length = read( fd, buffer, EVENT_BUF_LEN );
if ( length < 0 )
log->Print("Could not read inotify file descriptor");
else
{
....
检查https://github.com/paulorb/FileMonitor它有一个简单的界面来实现你想要的。它是一个使用inotify的windows API到Linux的端口。
的例子:
#include "FileMonitor.hpp"
int main(void)
{
int m_EventID = FindFirstChangeNotification("/media/sf_P_DRIVE/FileMonitor/", 0, FILE_NOTIFY_CHANGE_FILE_NAME);
int ret = WaitForSingleObject(m_EventID, 10000);
printf("nFinish %d", ret);
fflush(stdout);
FindNextChangeNotification(m_EventID);
int ret2 = WaitForSingleObject(m_EventID, 10000);
printf("nFinish %d", ret2);
FindCloseChangeNotification(1);
printf("nChangeNotification done");
fflush(stdout);
return 0;
}
如果你喜欢自己做,试着使用poll函数,你可以在线程中使用它。
struct pollfd pfd = { th_params->fd, POLLIN, 0 };
int ret = poll(&pfd, 1, 50); // timeout of 50ms
if (ret < 0) {
printf("failed poll");
}
else if (ret == 0) {
// Timeout with no events, move on.
printf("nTimeout poll");
}
else {
i = 0;
int lenght = read(th_params->fd, buffer, 1024);
}
void FilesListener::refresh()
{
char buffer[1024];
int length = read( fd, buffer, EVENT_BUF_LEN );
if ( length >=0 )
{
....