In a Cocoa application, I have this setup:
- The main thread (M) can send requests to some kind of background thread of the "producer" (B) to do some work, say, the result of the calculation on the element X.
- Otherwise, the results of computing the element X may appear in another background thread (C) and they will synchronously receive these results.
Thread C can simply perform the operation synchronously by itself, but if thread B is already in the middle of computing element X, I would like thread C to block and get the results from B. The calculation results can be found on disk, so data transfer is not a problem.
What is the best way to block thread C until thread B is executed using element X?
Note that the elements that are processed by B are arbitrary. X is just one of many elements in this example. I would like to block until element X is defined.
So, conceptually, what I need is a method of thread B, setting some kind of flag, when it starts saying βI'm working on Xβ, and if C comes in and sees this flag, it waits for the flag to clear, and then get the result .
Not sure if I can somehow switch NSLocks to this role or if the OS is better primitive.
Any thoughts (or potential rethinking of the problem) are welcome! Thank.