为什么要使用select()调用对inotify实例执行选择性读取
why perform selective read on the inotify instance using select() call
基于此http://www.thegeekstuff.com/2010/04/inotify-c-program-example/
示例:https://gist.github.com/pkrnjevic/6016356#file-示例cpp
while ( run )
{
select( fd + 1, &watch_set, NULL, NULL, NULL ); // non-blocking
int length = read( fd, buffer, EVENT_BUF_LEN );
if ( length < 0 )
{
perror( "read" );
}
与
while ( run )
{
int length = read( fd, buffer, EVENT_BUF_LEN ); // blocking
if ( length < 0 )
{
perror( "read" );
}
问题>我需要监视目录中的更改。上面的select+read和read ONLY方法之间的关键区别是什么。据我所知,select+read并没有阻止读取,它会一次又一次地轮询这个调用。这个精选+阅读真的给我带来了什么好处吗?
感谢
这段代码实际上是相同的。第一个版本将阻止直到描述符可读,然后读取(在读取时不会阻止)。第二个版本将阻止读取。第二个版本可能会更快,因为它只调用一个内核,而不是两个。
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 在std::vector上存储带有模板的类实例
- 为什么要使用select()调用对inotify实例执行选择性读取