How to perform multithreading using a single thread?

In a recent "technical discussion," I was asked, "how to do multithreading using a single thread?" Confirming that the interviewer did not address this issue, I had to admit that I did not have a good idea how to achieve multithreading in one thread, and, in addition, I find this question a bit controversial. The answer provided by the interviewer โ€œwith the help of the multicast delegateโ€ left me at a loss, maybe he really does not understand the delegates and the main stream processing. I would be interested to know if the question has any merit and, more importantly, if the corresponding answer makes sense. Thanks.

+6
source share
1 answer

Coroutines is something to simulate collaborative multithreading (.NET is not supported unless we consider the async / await pattern as a coroutine pattern).

Asynchronous programming imitates multithreading (at least partially ... Several read / write operations are performed at the same time) ... Both options are possible solutions that hide the "threading" part.

To develop asynchronous programming ... One could create a whole web server capable of simultaneously responding to hundreds of requests based on a single thread + asynchronous development. Each read from disk will be performed asynchronously. Each response to connecting clients will be performed asynchronously, etc.

To give a name, from what I understand, node.js is a single-threaded web server entirely based on asynchronous programming (technically called non-blocking I / O) ... See for example fooobar.com/questions/25352/ .. .

To what I wrote, I add that there are some languages โ€‹โ€‹that implement the so-called Green Themes . Green threads are shared threads that do not use the OS scheduler. Their code is so executed in one thread (at least from their point of view). It seems that Go, haskell, old Ruby, various versions of Smalltalk use / use green threads).

+4
source

Source: https://habr.com/ru/post/951893/


All Articles