非阻塞选择inotify

时间:2016-10-27 19:23:41

标签: c++ multithreading

我尝试在线程中的c ++中使用inotify 但是select是阻塞的,所以当我的应用程序退出时,我永远无法超越线程

我如何创建inotify手表

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
    {  
    ....

2 个答案:

答案 0 :(得分:1)

检查https://github.com/paulorb/FileMonitor它有一个简单的界面,可以实现您想要的效果。它是使用inotify的Linux API的一个端口。

示例:

#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);
        }

答案 1 :(得分:0)

void FilesListener::refresh()
{
    char buffer[1024];

    int length = read( fd, buffer, EVENT_BUF_LEN ); 
    if ( length >=0 ) 
    {  
    ....