Coroutine & Subroutine
Coroutineμ Co + Routineμ΄ ν©μ³μ§ λ¨μ΄μ΄λ€.
Corouine is a combination of Co + Routine.
Co : ν¨κ» ( Together )
Routine : κ·μΉμ μΌλ‘ νλ μΌμ ν΅μμ μΈ μμλ λ°©λ² ( The usual order or method of doing something regularly )
νλ‘κ·Έλλ°μμμ 루ν΄μ΄λΌκ³ νλ©΄ νΉμ ν μΌμ μ²λ¦¬νκΈ° μν μΌλ ¨μ λͺ
λ Ή νΉμ κ·Έ μμ μ λλ‘ μ 리ν μ μκ² λ€.
Routines in programming can be organized into a series of commands or orders to handle a specific task.
λν μ°λ¦¬λ μ΄λ¬ν μΌλ ¨μ λͺ
λ Ήμ ν¨μλΌκ³ νκΈ°λνλ€.
We also call this series of commands a Function.
μλΈλ£¨ν΄ ( What is Subroutine? )
νλ‘κ·Έλλ°μμ ν¨μ μμ ν¨μκ° μλ κ²½μ°, λ΄λΆμ μλ ν¨μλ₯Ό μλΈλ£¨ν΄μ΄λΌκ³ ν μ μλ€.
In programming, if there is a function within the function, the function inside is called a subroutine.
def out_function():
inside_function()
return
// Subroutine !
def inside_function():
passμ°λ¦¬κ° μΈλΆν¨μλ₯Ό νΈμΆνλ©΄ λ΄λΆν¨μλ μΈλΆν¨μμ μμμ μ€νλ κ²μ μ μ μλ€.
If we call out_function, we can expect inside_function to be called within out_function.
μ½λ£¨ν΄ ( What is Coroutine ? )
μ½λ£¨ν΄μ΄λ ν¨κ» μνλλ ν¨μμ΄λ€.
Coroutine is a function that runs together.

μΌλ°μ μΌλ‘ ν¨μμ μ§μ μ μ ν¨μμ μμ λΆλΆμ΄ λ κ²μ΄λ€. κ·Έλ¬λ μ½λ£¨ν΄μμλ μλλ€.
In general, the entry point of the function will be the beginning of the function. But not in Coroutine.
μ½λ£¨ν΄μ νλμ μ½λ£¨ν΄μ΄ λ€λ₯Έ μ½λ£¨ν΄ μμμ μνλλ κ²μ΄ μλλ€.
In Coroutine, one Coroutine is not performed in the other.
κ°κ°μ μλ‘ λ€λ₯Έ ν¨μμ΄λ©°, ν¨κ» μνλλ€. κ·Έλ¬λ λ€λ₯Έ νλ‘μΈμ€μΈ κ²μ μλλ€.
Each is a different function and is performed together. However, this does not mean that it is a different process, a different thread.
μ΄λ€μ΄ νλμ μ€λ λλ₯Ό μ μ νκ³ μμ λ, ν Routineμ΄ λ€λ₯Έ Routineμκ² μ€λ λ μ μ κΆνμ μ보ν¨μΌλ‘μ¨ ν¨κ» μνλλ€.
When they occupy one thread, it is done together by one Routine giving up the thread occupancy authority to another Routine.
μ¦, μΌλ° ν¨μλ ν λ² μ§μ νκ³ ν λ² λ¦¬ν΄νλ€λ©΄, μ½λ£¨ν΄μ μ¬λ¬ κ°μ μ§μ μ κ³Ό μ¬λ¬ κ°μ μ€λ¨μ μ κ°μ§ μ μλ€.
That is, if the general function enters once and returns once, the coroutine may have several entry points and several breakpoints(suspend point).
Returnμ ν¨μλ₯Ό μμ ν μ’
λ£νκ³ μ€ν λ©λͺ¨λ¦¬μ ν λΉλ λͺ¨λ 리μμ€λ₯Ό ν΄μ νλ κ²μ μλ―Ένμ§λ§, μ€λ¨μ ν λ©λͺ¨λ¦¬ μμμ λ€μ μ¬κ°(resume)νκΈ° μν΄ νμν λͺ¨λ μ 보 - μ½λ£¨ν΄ μ€ν
μ΄νΈ(coroutine state) - λ₯Ό μ μ₯νκ³ μ μ΄κΆμ νΈμΆμμκ² λ€μ λκΈ°λ κ²μ μλ―Ένλ€.
A return means shutting down the function completely and releasing all resources allocated to the stack memory, but suspend means saving all the information required to resume to the heap memory area - called the coroutine state - and handing control back to the caller.
κ·Έλ¦¬κ³ λκ΅°κ°κ° λ€μ Resumeμ μμ²μ νλ©΄ ν λ©λͺ¨λ¦¬ μμμ μ μ₯λμ΄ μλ μ½λ£¨ν΄ μ€ν
μ΄νΈμ μ 보λ₯Ό κ°μ Έμ 볡ꡬνμ¬, μ€λ¨ λμλ λ°λ‘ λ€μλΆν° ν¨μλ₯Ό λ€μ μ§ν ν©λλ€.
And when someone asks for a resume, it takes the information of the coroutine state that was stored in the heap memory area and recovers it, and then it goes back to the function right after it was interrupted.
λΉμ μ ν λ©ν°νμ€νΉκ³Ό μ μ ν λ©ν°νμ€νΉ
Non-preemptive Multitasking and Preemptive Multitasking
νλμ Taskκ° Schedulerλ‘ λΆν° CPU μ¬μ©κΆμ ν λΉ λ°μμ λ, Schedulerκ° κ°μ λ‘ CPU μ¬μ©κΆμ λΊμ μ μμΌλ©΄ λΉμ μ ν λ©ν°νμ€νΉμ΄κ³ , λΊμ μ μμΌλ©΄ μ μ ν λ©ν°νμ€νΉμ΄λ€.
When a task is assigned a CPU license by the scheduler, it is non-preemptive multi-tasking if the scheduler cannot forcibly take away the CPU license, and preemptive multi-tasking if it can.
μ½λ£¨ν΄μ λΉμ μ ν λ©ν°νμ€νΉμ΄κ³ , μ€λ λλ μ μ ν λ©ν°νμ€νΉμ΄λ€.
Corouine is βNon-preemptive Multitaskingβ and thread is βPreemptive Multitaskingβ
μ¦, μ½λ£¨ν΄μ λ³νμ±(Concurrency)μ μ 곡νμ§λ§ λ³λ ¬μ±(Parallelism)μ μ 곡νμ§λ μλλ€λ μλ―Έμ΄λ€.
That is, it means that coroutine provides concurrency but does not provide parallelism.
λ³νμ±κ³Ό λ³λ ¬μ± ( Concurrency and Parallelism )
Concurrency
It seems to be running at the same time.
It belongs to the Logical Level.
Using Single Core
It can operate in sequence rather than in parallel physically.
In fact, the CPU is divided into time-sharing so that the user can feel the concurrency.
Parallelism
It is actually done at the same time.
It belongs to the Physical (Machine) Level.
Only in Multi Core.
μ½λ£¨ν΄μ μ₯μ ( Advantage of using Coroutine )
μ½λ£¨ν΄κ°μ μμ κ΅ν λ λΉμ©μ΄ μ λ€λ κ²μ΄λ€.
It is said that the cost is low when exchanging work between coroutines.
μ°λ λκ° μμ κ΅νμ system call λλ blocking call μ½ λ±μ λΉμ©μ΄ λ°μνλ€.
Work exchange between threads occur costs such as system call or blocking call.
λκΈ°ν μμ μ μν mutexes, semaphores λ±μ μ₯μΉκ° νμμκ³ OSμ μ§μλ νμκ° μλ€
There is no need for devices such as mutexes and semaphores for synchronization work and no need for OS support
Last updated