Add Lyng deferred cancellation support
This commit is contained in:
1 parent
3e338f3d53
commit
311cf6ee44
8 files changed
+180
-66
No files matched your search
+16
-1
@@ -32,10 +32,25 @@ Depending on the platform, these coroutines may be executed on different CPU and
|
||||
assert(xIsCalled)
|
||||
>>> void
|
||||
|
||||
This example shows how to launch a coroutine with `launch` which returns [Deferred] instance, the latter have ways to await for the coroutine completion and retrieve possible result.
|
||||
This example shows how to launch a coroutine with `launch` which returns [Deferred] instance, the latter have ways to await for the coroutine completion, cancel it if it is no longer needed, and retrieve possible result.
|
||||
|
||||
Launch has the only argument which should be a callable (lambda usually) that is run in parallel (or cooperatively in parallel), and return anything as the result.
|
||||
|
||||
If you no longer need the result, cancel the deferred. Awaiting a cancelled deferred throws `CancellationException`:
|
||||
|
||||
var reached = false
|
||||
val work = launch {
|
||||
delay(100)
|
||||
reached = true
|
||||
"ok"
|
||||
}
|
||||
work.cancel()
|
||||
assertThrows(CancellationException) { work.await() }
|
||||
assert(work.isCancelled)
|
||||
assert(!work.isActive)
|
||||
assert(!reached)
|
||||
>>> void
|
||||
|
||||
## Synchronization: Mutex
|
||||
|
||||
Suppose we have a resource, that could be used concurrently, a counter in our case. If we won't protect it, concurrent usage cause RC, Race Condition, providing wrong result:
|
||||
|
||||
Reference in new issue
Block a user