跨线程事件信令(object - c)的同步/等待设计?

时间:2022-12-06 10:58:51

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

在一个Cocoa应用程序中,我有这样一个设置:

  1. The main thread (M) can submit requests to a some background "producer" thread (B) to get some work done, say the result of a computation on item X.
  2. 主线程(M)可以向某个后台“生产者”线程(B)提交请求,以完成某些工作,比如对项目X的计算结果。
  3. A different background thread (C) shortly thereafter might want the results of computing item X, and want those results synchronously.
  4. 不久之后,另一个后台线程(C)可能希望得到计算项X的结果,并希望同步地获得这些结果。

Thread C could just re-do the work synchronously itself, but if thread B happens to be in the middle of computing item X already, I would like thread C to block and get the results from B. The results of the computation are findable on disk, so the data passing is not the issue.

同步线程C可以重新工作本身,但如果线程B是在中间的计算项目X,我想线程C阻止并得到结果B的计算结果可发现的磁盘上,所以数据不是问题。

What's the best way of blocking thread C until thread B is done with item X?

在线程B对项目X完成之前,阻止线程C的最好方法是什么?

Note that there the items that B processes are arbitrary-- X is just one of many items in this example. I'd want to block until specifically item X is done.

注意,在这里B过程是任意的——X只是这个例子中的众多项中的一个。在具体的项目X完成之前,我想要阻止它。

So conceptually what I'd like is a way of thread B setting up some sort of flag when it starts saying "I'm working on X", and if C comes in and sees that flag, it waits for the flag to clear, and then gets the result.

所以概念上,我想要的是线程B在开始说“我在处理X”时设置某种标志,如果C进来看到那个标志,它会等待标志清除,然后得到结果。

Not sure if I can somehow shoehorn NSLocks into this role, or if there's a better primitive in the OS.

不确定我是否能以某种方式将nshorn锁定到这个角色中,或者OS中是否有更好的原语。

Any thoughts (or potential reframing of the problem) welcome! Thanks.

任何想法(或问题的潜在重构)欢迎!谢谢。

2 个解决方案

#1


4  

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.

如果可能的话,使用NSOperation的。它有一个-waitUntilFinished方法,允许同步计算。你只需要一些线程安全的存储机制来允许你找到你正在计算的项目的NSOperation,如果它已经存在的话——比如,NSLock保护NSDictionary。

#2


3  

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.

NSConditionLock在这里运行得很好。可能是与每个x相关联的条件锁。最初条件“休眠”,然后由后台线程设置为“处理”,然后在完成时设置为“完成”。调用线程可以检查“处理”条件,如果设置了,则等待条件“完成”。

#1


4  

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.

如果可能的话,使用NSOperation的。它有一个-waitUntilFinished方法,允许同步计算。你只需要一些线程安全的存储机制来允许你找到你正在计算的项目的NSOperation,如果它已经存在的话——比如,NSLock保护NSDictionary。

#2


3  

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.

NSConditionLock在这里运行得很好。可能是与每个x相关联的条件锁。最初条件“休眠”,然后由后台线程设置为“处理”,然后在完成时设置为“完成”。调用线程可以检查“处理”条件,如果设置了,则等待条件“完成”。