Chengpeng Hu
Chengpeng Hu

Reputation: 51

How to parallel a for loop with pthread in iOS

I am trying to parallel a simple for loop with pthread. All it needs to do is to access a global array by different thread.

    for(int i = 0, i < ary.count; i++){
    //do something with ary[i];
    }

How to do that?

Upvotes: 0

Views: 252

Answers (1)

rmaddy
rmaddy

Reputation: 318944

You can use NSArray enumerateObjectsWithOptions:usingBlock: to enumerate the elements of the array using multiple threads:

[ary enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id object, NSUInteger index, BOOL *stop) {
    // do something with object
}];

Upvotes: 1

Related Questions