当前位置:网站首页>Why should the coroutine be set to non blocking IO

Why should the coroutine be set to non blocking IO

2022-06-25 14:53:00 Mr . Solitary patient

Preface

It used to be thought that the coordination process was in read 、 write before yield Give up the process , Wait for the data to arrive resume Come back , Carry out subsequent processing , Suppose you use blocking io, I just need to be in eventloop Medium epoll_wait Set up timeout Still able to achieve asynchrony , therefore io Whether it is blocked or not has nothing to do with the whole process ( Anyway, you have to let it out before you get stuck ), But when I was writing code these two days, I thought it over carefully and found that this idea was wrong , The coroutine must use non blocking io.

reason

The reason for the above idea is that the multithreading of the program is not considered , When blocked io perform epoll_wait When , The current thread will switch from user mode to kernel mode , And suspend within the operating system , So the operating system goes back to executing other threads , Then the suspended thread will not be able to continue execution once there is a process timeout or the data processing is completed , Wait until epoll_wait Timeout and return to user status , And the operating system puts cpu Switch back to this thread to execute .

原网站

版权声明
本文为[Mr . Solitary patient]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200518050498.html