Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's harder, synchronizing 2 threads or 1000 threads?

On Paul Tyma's presentation, I found an interview question:

What's harder, synchronizing 2 threads or synchronizing 1000 threads?

From my perspective, of course synchronizing 1000 threads is harder, but I can't think of a good reasons for that beside 'of course'. But since it's interview question, may be I'm wrong (interview questions have to be tricky, isn't it?).

like image 913
nanda Avatar asked Jul 29 '10 12:07

nanda


3 Answers

You could make the case that synchronizing 2 threads correctly is in fact harder than doing it for 1000, because if you have a race condition, it will usually manifest very quickly with 1000 threads, but not so with only 2.

But on the other hand, synchronizing 1000 threads without running into lock contention issues is much harder than when there are only 2.

The real answer is "synchronizing threads is hard in various ways, period."

like image 91
Michael Borgwardt Avatar answered Nov 16 '22 03:11

Michael Borgwardt


Synchronizing a thousand threads is just as easy as synchronizing two threads: just lock access to all important data.

Now, synchronizing a thousand threads with good performance is more difficult. If I were asking this question, I'd look for answers mentioning "the thundering herd problem", "lock contention", "lock implementation scalability", "avoiding spinlocks", etc.

like image 34
Borealid Avatar answered Nov 16 '22 02:11

Borealid


In an interview, I would say that "exactly two threads" is a very useful special case of multi-threading. Things like starvation and priority inversion can occur with as few as three threads, but with only two threads priority inversion and starvation can never occur (well, starvation could occur if a thread released and reacquired a lock without letting the other thread start, but with three threads starvation can occur even if locks are grabbed instantly when available). Going from 2 threads to 3 is a bigger jump than going from 3 to 1,000.

like image 31
supercat Avatar answered Nov 16 '22 02:11

supercat