6

In a recent “technical discussion”, I was asked “how do you accomplish multithreading using a single thread?” After confirming that the interviewer did not consider this a trick question, I had to admit that I didn’t have a good idea how to achieve multithreading on a single thread, and further, that I considered the question a bit of a contradiction. The answer the interviewer provided, “by using a multicast delegate,” left me wondering if maybe he didn’t really understand delegates and the underlying threading. I would be interested to know if the question has any merit and, more importantly, if the associated answer makes any sense. Thank you.

Seymour
  • 7,043
  • 12
  • 44
  • 51
  • 2
    *trick question*, though :). – cuongle Aug 16 '13 at 14:58
  • 2
    Yes; he has no idea what he's talking about. The correct answer is fibers. – SLaks Aug 16 '13 at 14:58
  • 3
    @SLaks I'd say that's still not multithreading. –  Aug 16 '13 at 14:58
  • @hvd: Why not? (or, more generally, user-mode schedulers) – SLaks Aug 16 '13 at 14:59
  • 3
    The correct response is to explain to him that multicast delegates are executed sequentially. – SLaks Aug 16 '13 at 15:00
  • @SLaks Because if one "thread" gets stuck (say, in an accidental infinite loop), nothing else can run. Real multithreading would only let that one thread get stuck. –  Aug 16 '13 at 15:00
  • You're correct, he had no clue what is he talking about. If you can't deadlock it - it's not a thread – Sten Petrov Aug 16 '13 at 15:01
  • @hvd Cooperative multithreading is still multithreading, but one thread can look everything – xanatos Aug 16 '13 at 15:01
  • @xanatos In that case, I think we just use the same word for different things. Multitasking is a name I'd have no problems with. –  Aug 16 '13 at 15:03
  • As a funny sidenote, the first versions of Java on Solaris used only cooperative multithreading (see for example http://en.wikipedia.org/wiki/Green_threads) (aka Green Threads) – xanatos Aug 16 '13 at 15:17

1 Answers1

4

Coroutines are something done to simulate cooperative multithreading (not supported by .NET, unless we consider the async/await pattern to be a coroutine pattern).

Asynchronous programming simulates multithreading (at least partially... More than one read/write for example is executed at the same time)... Both are possible solutions that hide the "threading" part.

To elaborate on the asynchronous programming... One could build an entire web server, able to respond to hundred of requests at the same time, based on a single thread + asynchronous elaboration. Each read from the disk would be done asynchronously. Every response to the connecting clients would be done asynchronously and so on.

To give a name, from what I comprehend, node.js is a single threaded web server entirely based on asynchronous programming (technically called non blocking I/O)... See for example https://stackoverflow.com/a/14797359/613130

To what I've written, I'll add that there are some languages that implement what is called Green threads. Green threads are cooperative threads that don't use the OS scheduler. Their code so is executed in a single thread (at least from their point of view). It seems that Go, haskell, old Ruby, various versions of Smalltalk all use/used green threads).

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 1
    asynchronous != multithreaded. the main difference there is that for the asynchronous code it's only able to run an entire "block" of code, all at once, and it can only swap out to some other section of code when another entire block finishes. In a multithreaded environment it can pause the execution of a stack, swap it out with another context, and then go back to the previous context. I can't see how C# would be able to accomplish that; it's too high of a level to accomplish it without the help of threads. – Servy Aug 16 '13 at 15:21
  • 1
    @Servy What I meant is that from the external observer perspective, asynchronous == multithreaded: more than one request can be processed by Node.js at the same time. And in truth asynchronous is similar to multithreaded, because you are leaving the operation to the OS that will execute it on one of its threads. So, instead of being multithreading "inside" the process, it's multithreading "across the process and the OS". Yes, it's splitting the proverbial fiber in 4, but the question was a semi-trick question that required at least a little of lateral thinking. – xanatos Aug 16 '13 at 15:35
  • 1
    But the fact remains that this isn't multithreading. This could mean that the person asking the question actually meant "asynchronous" and not "multithreading". If they did, then this would be an answer. – Servy Aug 16 '13 at 15:37
  • To summarize, if the question meant to ask about the asynchronous nature of multithreading, then a Multicast Delegate would be an acceptable solution. If on the other hand, the question was really attempting to address both asynchronous and concurrent multithreading, then there really is no practical “multithreading-on-a-single-thread” .Net solution. Thanks. – Seymour Aug 19 '13 at 19:53
  • 1
    @adkSerenity No, Multicast Delegate isn't even an asynchronous solution. Or at least I wouldn't define it that way. It isn't anymore asynchronous than a list of delegates. – xanatos Aug 19 '13 at 19:58
  • I’m tracking … Thanks all for sharing your expertise. – Seymour Aug 20 '13 at 12:28
  • When you use async/await the remainder of your method (following the await) will run on a threadpool thread. For that reason, async/await is not single-threaded but its multi-threaded nature is abstracted away from the developer. Perhaps the interviewer was looking for "Windows Fibres" as a response? Which are co-operatively scheduled on (potentially) a single thread. – David Dec 20 '13 at 12:12