中央策略打开多个文件 [英] Grand Central Strategy for Opening Multiple Files

查看:104
本文介绍了中央策略打开多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用大中央调度队列(1)打开一个文件,并在队列1计算一个OpenSSL的DSA散列的工作实现;(2)写出的哈希为在以后的验证新的边车的文件队列2。

I have a working implementation using Grand Central dispatch queues that (1) opens a file and computes an OpenSSL DSA hash on "queue1", (2) writing out the hash to a new "side car" file for later verification on "queue2".

我想在同一时间打开多个文件,但基于某种逻辑不呛操作系统由具有打开的文件和数百超过了硬盘的持续输出。图片浏览应用,如iPhoto或Aperture似乎打开多个文件,并显示他们,所以我假定这是可以做到。

I would like to open multiple files at the same time, but based on some logic that doesn't "choke" the OS by having 100s of files open and exceeding the hard drive's sustainable output. Photo browsing applications such as iPhoto or Aperture seem to open multiple files and display them, so I'm assuming this can be done.

我假设最大的限制将是磁盘I / O,因为应用程序可以(在理论上)同时读写多个文件。

I'm assuming the biggest limitation will be disk I/O, as the application can (in theory) read and write multiple files simultaneously.

有什么建议?

TIA

推荐答案

您在你会出现I / O密集​​型,最稳妥正确的。它将由具有多个文件打开,并在同一时间被积极读的随机存取性质配混

You are correct in that you'll be I/O bound, most assuredly. And it will be compounded by the random access nature of having multiple files open and being actively read at the same time.

因此​​,你需要取得一个有点平衡。更可能的情况是,一个文件是不是最有效的,因为你所观察到的。

Thus, you need to strike a bit of a balance. More likely than not, one file is not the most efficient, as you've observed.

个人?

我会使用一个调度信号。

I'd use a dispatch semaphore.

是这样的:

@property(nonatomic, assign) dispatch_queue_t dataQueue;
@property(nonatomic, assign) dispatch_semaphore_t execSemaphore;

- (void) process:(NSData *)d {
    dispatch_async(self.dataQueue, ^{
        if (!dispatch_semaphore_wait(self.execSemaphore, DISPATCH_TIME_FOREVER)) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                ... do calcualtion work here on d ...
                dispatch_async(dispatch_get_main_queue(), ^{
                    .... update main thread w/new data here ....
                });
                dispatch_semaphore_signal(self.execSemaphore);
            });
        }
    });
}

凡被拉开序幕:

self.dataQueue = dispatch_queue_create("com.yourcompany.dataqueue", NULL);
self.execSemaphore = dispatch_semaphore_create(3);
[self process: ...];
[self process: ...];
[self process: ...];
[self process: ...];
[self process: ...];
.... etc ....

您需要确定您想如何最好地处理排队。如果有许多项和有消除的概念,入队一切都是可能的浪费。同样的,你可能会想排队URL以要处理的文件,而像上面没有NSData的对象。

You'll need to determine how best you want to handle the queueing. If there are many items and there is a notion of cancellation, enqueueing everything is likely wasteful. Similarly, you'll probably want to enqueue URLs to the files to process, and not NSData objects like the above.

在任何情况下,上面会同时处理三件事情,不管有多少已经被排队。

In any case, the above will process three things simultaneously, regardless of how many have been enqueued.

这篇关于中央策略打开多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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