I think I have discovered a pattern but I don't know the name of it. I very much doubt I made a genuine invention here.
In pseudo-code:
queue(closure) = {
if (closure.isRunning)
suspend
return deferred
else
result = closure.run()
deferred.notify(result)
return result
}
// usage:
result = queue { slow_computation() }
What queue
does is:
- checks if there already is a running
slow_computation()
- If there is, it suspends/blocks the thread and awaits for the previous result.
- If there isn't, it runs
slow_computation()
and notifies the suspended/blocked threads about the result.
Unlike plain result cache, the "cache" is discarded ASAP. Unlike plain operation queue, the result of a slow computation is reused. It reuses the result when there is contention, but not otherwise.
The question is, can you name this pattern?
Aucun commentaire:
Enregistrer un commentaire