Suppose a class that's arranged something like this:
class A {
func a() {
doStuff()
}
func b() {
Task {
something()
await someLongRunningThing()
somethingElse()
}
}
func c() {
doOtherStuff()
}
}
How can I make sure that only one function is ever active at a time and that the execution of each function is in a FIFO manner? This would include the body of the Task
in b()
. If that Task
is executing, none of the other functions of the class should be able to be entered and they would queue up and operate FIFO with no interleaving.
Since the member functions will probably be called on the main thread, there should be no blocking of a thread involved.
This is a common scenario I face, and I can't think of a reasonable solution. At first I was excited about actor
, but the actor allows reentry when there's an await
in a function, allowing race conditions. The only thing I can find about this is here. But here, the author really just pushes the problem further up the call hierarchy.