English 中文(简体)
我如何能够保证这一功能在二读结束工作之前恢复价值?
原标题: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;

我愿从operation结束前获得Value。

最佳回答

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.

问题回答

暂无回答




相关问题
Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Is reading from an XmlDocument object thread safe?

I was wondering if i could safely read from an XmlDocument object using SelectNodes() and SelectSingleNode() from multiple threads with no problems. MSDN says that they are not guaranteed to be ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签