请考虑以下以串行/顺序方式读取文件数组的代码.readFiles
返回一个promise,只有在按顺序读取所有文件后才会解析.
var readFile = function(file) {
... // Returns a promise.
};
var readFiles = function(files) {
return new Promise((resolve, reject) =>
var readSequential = function(index) {
if (index >= files.length) {
resolve();
} else {
readFile(files[index]).then(function() {
readSequential(index + 1);
}).catch(reject);
}
};
readSequential(0); // Start!
});
};
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但我不喜欢按顺序进行递归递归.是否有一种更简单的方法可以重写此代码,以便我不必使用我的奇怪readSequential
功能?
最初我试图使用Promise.all
,但这导致所有readFile
调用同时发生,这不是我想要的:
var readFiles = function(files) {
return Promise.all(files.map(function(file) {
return readFile(file);
}));
};
Run Code Online (Sandbox Code Playgroud) 是否可以通过将NSoperationQueue
对象设置maxConcurrentOperationCount
为1 来将对象用作串行FIFO队列?
我注意到文档声明......
对于最大并发操作数设置为1的队列,这相当于一个串行队列.但是,您永远不应该依赖于操作对象的串行执行.
这是否意味着无法保证FIFO执行?
Apple的Grand Central Dispatch(GCD)非常棒,但仅适用于iOS 4.0或更高版本.Apple的文档说,"[A]序列化操作队列不提供与Grand Central Dispatch中的串行调度队列完全相同的行为"(因为队列不是FIFO,但顺序由依赖关系和优先级决定).
在GCD发布之前支持操作系统版本的同时,实现与GCD的串行调度队列相同效果的正确方法是什么?或者换句话说,在想要支持低于4.0的版本的iOS应用程序中处理简单后台处理(执行Web服务请求等)的推荐方法是什么?
objective-c nsoperationqueue grand-central-dispatch ios serial-processing
我有一个方法,有时可以在我的代码中调用.下面是一个非常基本的示例,因为代码处理iphone照片库中的图像和文件,并在完成该方法时标记它们已经处理过.
@property (nonatomic, assign) dispatch_queue_t serialQueue;
....
-(void)processImages
{
dispatch_async(self.serialQueue, ^{
//block to process images
NSLog(@"In processImages");
....
NSLog(@"Done with processImages");
});
}
Run Code Online (Sandbox Code Playgroud)
我认为每次调用此方法时我都会得到以下输出..."在processImages""使用processImages完成""在processImages""完成了processImages"等...
但我总是得到
"在processImages""在processImages""完成与processImages""完成与processImages"等...
我以为串口队列会等到第一个块完成,然后开始.对我来说,似乎它正在启动方法,然后它再次被调用并在第一个调用完成之前启动,创建通常不会被处理的图像的重复,因为如果它真的按顺序执行,则该方法将知道它们已经处理完毕.也许我对串行队列的理解并不具体.有什么输入?谢谢.
编辑:更多上下文,这是块中发生的事情...这可能导致问题???
@property (nonatomic, assign) dispatch_queue_t serialQueue;
....
-(void)processImages
{
dispatch_async(self.serialQueue, ^{
//library is a reference to ALAssetsLibrary object
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
....
//Process the photos here
}];
failureBlock:^(NSError *error) { NSLog(@"Error loading images from library");
}];
});
}
-(id)init
{
self …
Run Code Online (Sandbox Code Playgroud) 我是新来的,而且我在编程方面也相对较新.我用C编写了一个程序,我需要使用pthread来加速它.我尝试使用OpenMP这样做,但我不知道如何调试它.此外,我需要找出程序是否使用pthreads和时间更快,但我不知道如何在我的代码中写这个.这是我的代码
enter code here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#define NTHREADS 2
#define FYLLO(komvos) ((komvos) * 2 + 1)
long factorial(long);
void heap_function (int [], int, int );
void make_heap(long [], int );
void pop_heap(long [], int );
struct thread_data
{
long int n;
long int k;
long *b;
};
main()
{
long int n,k,c,fact=1;
long *a,*b,*d,p[k];
int i,j,rc;
int q[]={2,3,4,5,6,7,8,9,12,13,14,15,16};
pthread_t thread[NTHREADS];
struct thread_data threada;
for(i=0;i<NTHREADS;i++)
{
threada.n=n;
threada.k=k;
threada.b=b;
pthread_create (&thread[i], NULL, (void *)&threada);
} …
Run Code Online (Sandbox Code Playgroud) c parallel-processing performance pthreads serial-processing
ios ×3
objective-c ×2
c ×1
fifo ×1
javascript ×1
performance ×1
promise ×1
pthreads ×1
q ×1
queue ×1
sequential ×1