I have an object, myObject, which is going to spawn a number of NSOperations. Let’s say N of them. Each NSOperation will have an int idNumber from 0 to N-1, and a reference to myObject.
The NSOperations each calculate a float. I want myObject to store the floats, and do something when all N operations are done. So I am wondering if it is thread-safe to have as instance variables:
float results[N];
int numFinished;
And a method
So each NSOperation calls [myObject operationDone:self] when it is done calculating.
I guess my question has two parts: is it safe to modify results from multiple threads provided the "i" is different for each thread? And is it safe to increment numFinished from multiple threads the way I describe?
If not, what is a good way to track the results of N operations and do some work when they are all finished?
Edit: In case it matters, myObject will itself have been spawned by an NSOperation.
Edit 2: I could, of course, make a dummy method for the operations to call, which just calls operationDone on the main thread. However, I do not want to call [myObject processAllResults] on the main thread because it will take a while. Is a solution then:
Edit 3: Ideally I’d prefer to perform the selector not on the main thread, but on the particular thread running the NSOperation that spawned myObject. If I have a reference to that NSOperation, is there a way to fetch the thread it is running on?
The NSOperations each calculate a float. I want myObject to store the floats, and do something when all N operations are done. So I am wondering if it is thread-safe to have as instance variables:
float results[N];
int numFinished;
And a method
Code:
-(void)operationDone:(id)sender
{
results[ [sender idNumber] ] = [sender result];
if (++numFinished == N) [self processAllResults];
}
So each NSOperation calls [myObject operationDone:self] when it is done calculating.
I guess my question has two parts: is it safe to modify results from multiple threads provided the "i" is different for each thread? And is it safe to increment numFinished from multiple threads the way I describe?
If not, what is a good way to track the results of N operations and do some work when they are all finished?
Edit: In case it matters, myObject will itself have been spawned by an NSOperation.
Edit 2: I could, of course, make a dummy method for the operations to call, which just calls operationDone on the main thread. However, I do not want to call [myObject processAllResults] on the main thread because it will take a while. Is a solution then:
Code:
-(void)operationDone:(id)sender
{
[self performSelectorOnMainThread:@selector(updateResults)
withObject:sender
waitUntilDone:YES];
if (numFinished == N) [self processAllResults];
}
-(void)updateResults:(id)sender
{
results[ [sender idNumber] ] = [sender result];
numFinished = numFinished + 1;
}
Edit 3: Ideally I’d prefer to perform the selector not on the main thread, but on the particular thread running the NSOperation that spawned myObject. If I have a reference to that NSOperation, is there a way to fetch the thread it is running on?
Last edited: