Synchronization/wait design for cross-thread event signaling (Obj-C)?

后端 未结 2 463
太阳男子
太阳男子 2021-01-14 17:52

In a Cocoa app, I have a setup like this:

  1. The main thread (M) can submit requests to a some background \"producer\" thread (B) to get some work done, say the r
相关标签:
2条回答
  • 2021-01-14 18:03

    If possible, use NSOperation. It has a -waitUntilFinished method to allow for synchronous computation. You'd just need some thread-safe storage mechanism to allow you to find the NSOperation for the item you're computing, if it already exists—say, an NSLock guarding an NSDictionary.

    0 讨论(0)
  • 2021-01-14 18:12

    An NSConditionLock could work nicely here. Perhaps a condition lock associated with each X. Initially condition "dormant" then set to "processing" by the background thread then set to "complete" when it is done. The calling thread could check for the "processing" condition and if it is set, wait until condition "complete" is reached.

    0 讨论(0)
提交回复
热议问题