How can I guarantee that function returns a value before created secondary thread ended its work?
NSOperation *operation = /*some operation开发者_如何学运维*/;
[operationQueue addOperation:operation];
// …
// Some work
// …
return Value;
I want to get Value from function before operation ends.
As far as I know, you can't.
You may wait for an operation to be done, but trying to preempt a threadpool task is somewhat pointless.
Even if you managed to pause/suspend the queue before you add your operation
, you still have to resume it before you return
, and you have no way to avoid a possible context switch at this point.
If the Value
being returned is a shared var/global/field which might get modified by the operation, you may copy/clone its current value into a tempvar/local before the addOperation
to return it later.
精彩评论