https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

Terry Hope

12/06/2023, 10:06 PM
Copy code
suspend fun apple() {
        "some work"
        banana() // suspension point?
        delay(2) // suspension point
        "some work"
        yield() // suspension point
    }

    suspend fun banana() {
        "doing some work"
    }
A suspension point — is a point during coroutine execution where the execution of the coroutine may be suspended. Syntactically, a suspension point is an invocation of suspending function, but the actual suspension happens when the suspending function invokes the standard library primitive to suspend the execution. Help me understand this please. In the above code sample does the call to banana() is potentially suspension point even thought banana itself does not have any suspend points inside?
b

bezrukov

12/06/2023, 10:17 PM
No, banana call is not an actual suspension point, so 1. Caller's execution won't be suspended when calling it 2. Cancellation wont be thrown
1
t

Terry Hope

12/06/2023, 10:31 PM
thanks
k

kevin.cianfarini

12/06/2023, 11:02 PM
Suspension points bottom out in suspendCancellableCoroutine in the kotlinx library. This is the thing that actually suspends the coroutine you’re on. Likewise, this call checks for cancellation as well.
This function throws a CancellationException if the Job of the coroutine is cancelled or completed while it is suspended.
every suspend function that actually suspends and is cancellable ends up calling that function. Even ‘special’ ones like delay.
2 Views