Grand Central Dispatch(GCD)调度源标志 [英] Grand Central Dispatch (GCD) dispatch source flags

查看:89
本文介绍了Grand Central Dispatch(GCD)调度源标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近改用 kqueue GCD调度源以监视文件更改。这已经很好地完成了,并且产生了一个更简单的API。我记录了我的开关这里。我唯一的问题是,现在我无法访问我能够以kqueue进行的事件的标志。例如,使用kqueue,我能够检查文件是否已被删除,重命名,或者其属性是否已更改为:

I recently switched from using kqueue to GCD dispatch sources to monitor file changes. This has worked out well and resulted in a much simpler API. I documented my switch here. The only issue I have is that now I cannot access the flags on the event that I was able to in kqueue. For example with kqueue I was able to check if the file was deleted, renamed, or it's attributes were changed with the following:

struct kevent event;

...

if(event.flag & EV_DELETE)
{
    printf("File was deleted\n");
}

此API不适用于GCD,还是我需要设置调度源我希望听到的每一面旗帜。或者最好使用kqueue,因为它为已发生的事件提供了更大的可见性。

Is this API not available with GCD or do I need to set up dispatch sources up for each flag I would like to listen to. Or is it best to use kqueue since it provides greater visibility to the event that has occurred.

推荐答案

我找到了答案并发编程指南。我首先查看了 GCD参考但没有运气。指南中的相关行是

I found the answer in the Concurrency Programming Guide. I had first looked in the GCD Reference but without luck. The relevant line from the guide was


对于监视文件系统活动的描述符调度源,此函数返回一个指示事件类型的常量发生了。有关常量列表,请参阅dispatch_source_vnode_flags_t枚举类型。

For a descriptor dispatch source that monitors file system activity, this function returns a constant indicating the type of event that occurred. For a list of constants, see the dispatch_source_vnode_flags_t enumerated type.

以下是如何使用它的示例。

Here is an example of how you can use it.

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
int fildes = open("path/to/some/file", O_EVTONLY);
__block dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE,fildes, 
                                                  DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_ATTRIB | DISPATCH_VNODE_LINK | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
                                                  queue);
dispatch_source_set_event_handler(source, ^
{
    unsigned long flags = dispatch_source_get_mask(source);
    if(flags & DISPATCH_VNODE_DELETE)
        printf("DISPATCH_VNODE_DELETE\n");
    if(flags & DISPATCH_VNODE_WRITE)
        printf("DISPATCH_VNODE_WRITE\n");
    if(flags & DISPATCH_VNODE_EXTEND)
        printf("DISPATCH_VNODE_EXTEND\n");
    if(flags & DISPATCH_VNODE_ATTRIB)
        printf("DISPATCH_VNODE_ATTRIB\n");
    if(flags & DISPATCH_VNODE_LINK)
        printf("DISPATCH_VNODE_LINK\n");
    if(flags & DISPATCH_VNODE_RENAME)
        printf("DISPATCH_VNODE_RENAME\n");
    if(flags & DISPATCH_VNODE_REVOKE)
        printf("DISPATCH_VNODE_REVOKE\n");
});
dispatch_source_set_cancel_handler(source, ^(void) 
{
    close(fildes);
});
dispatch_resume(source);

这篇关于Grand Central Dispatch(GCD)调度源标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆