gpt4 book ai didi

c++ - inotify 的非阻塞选择

转载 作者:行者123 更新时间:2023-11-30 05:21:14 26 4
gpt4 key购买 nike

我尝试在线程内的 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);
}

关于c++ - inotify 的非阻塞选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com