Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if a non-suspend function is called from a coroutine?

One of the key concepts of coroutines in Kotlin is that a suspend function must be called from a coroutine or another suspend function.

However, a suspend function can call any kind of function, suspend or normal.
What is the consequence of that? (This is not a real scenario i have, just want to know for theoretical reasons)

I imagine that in that scenario the only point of creating a coroutine would be to change the context (thread) before calling it, so it doesn´t block the main thread. However, would all the other advantages of coroutines be lost? (cooperative cancellation, structured concurrency...)

like image 492
Gio Avatar asked Dec 27 '19 15:12

Gio


People also ask

Can you run a suspend function outside of a coroutine or another suspending function?

The syntax of a suspending function is similar to that of a regular function except for the addition of the suspend keyword. It can take a parameter and have a return type. However, suspending functions can only be invoked by another suspending function or within a coroutine.

What is the difference between suspending vs blocking?

Hunting to know BLOCKING vs SUSPENDINGA process is blocked when there is some external reason that it can not be restarted, e.g., an I/O device is unavailable, or a semaphore file is locked. A process is suspended means that the OS has stopped executing it, but that could just be for time-slicing (multitasking).

What is the use of Suspend function in Kotlin?

Suspend function is a function that could be started, paused, and resume. One of the most important points to remember about the suspend functions is that they are only allowed to be called from a coroutine or another suspend function.

Are coroutines blocking?

Suspending a coroutine does not block the underlying thread, but allows other coroutines to run and use the underlying thread for their code. runBlocking is also a coroutine builder that bridges the non-coroutine world of a regular fun main() and the code with coroutines inside of runBlocking { ... } curly braces.


1 Answers

If a suspending function calls another suspending function, then the coroutine is suspended, until the result is returned.

Calling a regular function from a suspending function will block the thread. Which thread? Well, that depends on the Dispatcher you're using. IO is able to spawn hundred of threads. But the Default dispatcher has same amount of threads as your CPUs count. Meaning that while this won't block other coroutines, it will reduce the amount of available resources.

Meaning: don't invoke non-suspending function that may block for a long period of time on this dispatcher, same as you don't block your UI thread.

And yes, suspending function may produce the same results, if you're do something like a busy loop without yield() or any other suspend invocation in it.

like image 177
Alexey Soshin Avatar answered Oct 27 '22 00:10

Alexey Soshin