+ 1

What is the main concept of Coroutines ?

18th Dec 2016, 8:42 PM
Nurah Alfaiz
Nurah Alfaiz - avatar
2 ответов
+ 2
Ok it's going to be a long answer haha ...Think of a scenario where you have an application that runs some kind of a UI loop that keeps everything together and suddenly you want to create a function that downloads some data from the web, That kind of process will probably take a few seconds, and therefore it will hold the code execution there and you're UI interface will be stuck until the process is done (because it's a blocking operation). Coroutines can help you deal with it by going back to the calling function from the coroutine when starting the problematic process, here you can "tell" the calling function to keep checking if the data was downloaded by going back to the previous point in the coroutine until the data was downloaded and then the "yield->check" loop ends and the code continues to run without getting blocked by the data download. this is NOT threading since the routines aren't running parallel to each other, they simply pass the control between each other (on the same thread).
21st Dec 2016, 1:49 AM
Yuval Keren
Yuval Keren - avatar
0
The main goal of Coroutines is to make asynchronous code look like synchronous code and therefore be more readable. The problem arise when you have a task that is blocking for a long period of time. For example when reading data from the network. You have to connect to the server, then read the data and then handle it. However the connect and read operation can take a lot of time. If you do that synchronously, your program will freeze while waiting fo the data. pseudo code: void readData(server) { socket = connect(server); // block for a long time. data = read(socket); // block for a long time. handle(data); } The solution to that is to use asynchronous operations. To do that you connect to the server but instead of waiting for the connection to be establish you just give a callback to execute once the connection is established. In this callback you read the data but instead of waiting for the data to be read you just give a callback to execute once the data is ready. This work fine but produce code that is less readable and more error prone. pseudo code: void readData(server) { return connect(server).then( [](socket) { // Once the connection is establish execute this. read(socket).then( [](data) { // Once the read is finished execute this. handle(data); } ); } ); } Coroutines lets you write code as if it were synchronous code but have the same behavior than asynchronous code. pseudo code: void readData(server) { socket = await connect(server); // suspend execution (~ return) until connect is done. // resume execution here when connected. data = await read(socket); // suspend execution until read is done. // resume execution when data is ready. handle(data); } For more information you can watch this: https://youtu.be/ZTqHjjm86Bw
27th Dec 2016, 10:47 AM
Simon Lepasteur
Simon Lepasteur - avatar