views:

726

answers:

9

Someone said 90% of programmers cannot solve a binary search.

So for a guy who claimed he has in depth understanding on multi-threading technology, what is the basic but ideal question to ask?

It can be either C++ or Java.

+2  A: 

I bet, a question to show a simple practical example of thread interference will weed out 80% of your specialists. Something like this.
But basically, you can ask about any common multithreading problem, like deadlock or race condition.

I would be careful with going into specific questions, though (e.g., how exactly one uses semaphores in C or atomic classes in Java), as I'm not sure that having whole API in your memory shows your understanding of underlying concepts.

Nikita Rybak
I would also have an example of these problems, and ask the person to prove that there is a problem by showing an example interleaving of threads which causes it. A classic deadlock problem would be the dining philosophers problem, which has the advantage of being very simple but involving more than two threads.
Brian
This reminds me `public void increment() {c=c++;}`.
Margus
+1  A: 

In Java, ask them what the difference between public synchronized void foo() is and public static synchronized void foo() in terms of locks acquired and what happens in differenct scenarios.

Also, see if they know what benefits the new lock objects in Java provide and also what good things like AtomicInteger are. You should basically ask anything from Java Concurrency in Practice if they claim to have an in depth understanding.

SB
It may even be worth splitting the questioning into two categories... concurrent designs/implementations/solutions and the Java Memory Model.
James Schek
why the downvote?
SB
A: 

I think you could ask the candidate to describe a standard race condition in a program, and how he would solve it.

You could also describe a basic situation hinting at a deadlock (use multiple threads trying to create/write into a file failing some of the time) and have him describe reasons for why that could be happening.

That's for basics, complicated stuff could be sharding problems, network on udp with threads blocking for response, etc...

samy
A: 

A few that quickly came to mind:

For Java:

  • What is a deadlock?
  • How can we prevent deadlocks? (ans: impose lock ordering)
  • How can lock ordering be imposed?

ans: lock objects based on some unique property. eg:

 if (a.id() < b.id()) {
        synchronize(a) {
            synchronize(b) {
                x();
            }
         }
     } else {
        synchronize(b) {
            synchronize(a) {
                 x();
            }
        }
     }     
  • What is an open method call? Why is that important? (ans: passing this to an object without holding a lock on this. prevents subtle lock ordering problems).
  • How to reduce lock granularity? (ans: lock splitting and lock stripping)
  • What is a thread pool and how to design one?
  • Alternative to custom designed thread pool. (ans: Executor framework)

C/C++:

  • Why is it theoretically impossible to write thread safe programs in C/C++? (ans: no std memory model)

General:

  • What is scalability? (ans: ability of a system to improve throughput when additional computing resources are added).
  • How to measure the speed up acquired by a system with additional processing resources? (ans: Amdahl's law).
Vijay Mathew
I'd have to say that the last question is extremely loaded. Instead, you should ask them "Why are memory fences important when writing thread safe code?". Even then, thats pretty deep stuff and I'm not sure if its on the level of binary search, especially if you can ensure that whatever multi-threaded library you're using is using memory fences. After all, if I got that question, I'd say "Erm... the Linux kernel is pretty thread safe".
Dragontamer5788
Also, the "Open Method Call" is a terminology question. You want to know the interviewee knows the concepts, and not the terminology. There are also many ways to cause deadlocks, even without threads. For example, http://stackoverflow.com/questions/2797087/java-deadlock-problem . IE: You need to keep blocking calls in mind too.
Dragontamer5788
@Dragontamer5788 Changed *technically* to *theoretically*. I guess that makes the question a fair one.
Vijay Mathew
@Dragontamer5788 Of course, the interviewer can explain the terminology. It is important that someone writing mult-threaded Java code is familiar with that concept.
Vijay Mathew
Every decent C / C++ implementation will have a decent implementation of pthread_mutex. Andd those pthread_mutex calls probably will be properly fenced. Again, its a loaded question. If you really want to catch a good grade C/C++ muti-threaded programmer, ask an open ended question without attacking the language he probably enjoys writing in. Especially when C++0x has a standard multithreaded memory model (atomics and such)
Dragontamer5788
It is possible to write thread safe programs in C++. Use a single set of threading APIs (e.g. pthreads) in a consistent way and pay heed of documentation referring to C/C++ libs. Of course that's easier said than done when you have 3rd party code, but it is possible to do. Furthermore, just programming in Java or .NET is no guarantee of thread safety either (e.g. Swing isn't multi-threaded and has various hacks to work around that) although they do have built-in intrinsics which at least ensure multithreading is somewhat easier to do.
locka
Vijay Mathew is asking something deeper here (or at least, I hope he is). Without compiler-supported memory barriers, C/C++ compilers are free to over-optimize your code and "move" reads / writes across function calls. Of course... the real answer is to use a compiler that understands memory fences and to use libraries that use those memory fences. *Standard* C/C++ doesn't have memory fences (although C++0x will have ordered atomics, which are sufficient). See this post: http://lists.boost.org/Archives/boost/2008/11/144803.php .
Dragontamer5788
@Vijay Mathew et al: Actually, it's theoretically impossible *not* to write thread safe code in C (and C++ ?) because the language has no support to even start threads.
JeremyP
There is the problem that some optimizations that compilers do which are perfectly safe for a serial program, might introduce data races when run in a multi-threaded environment. See e.g. http://stackoverflow.com/questions/2001913/c0x-memory-model-and-speculative-loads-stores
janneb
A: 

If it was me, I'd ask him to explain what "unsafe publishing" meant, or why the double-checked locking idiom is broken in Java. These should be common knowledge to anyone with "in depth experience" in Java concurrent programming.

(The answers to both these questions are in "Java Concurrency in Practice" by Brian Goetz et al. This is required reading for any Java software engineer, IMO.)

Stephen C
+7  A: 

One that I've been asked: implement a producer-consumer queue.

It covers both the proper use of synchronization/mutexes in your language of choice, as well as the likely bug of retaining those mutexes for longer than your want.

Of course, you have to understand how to write one yourself for this to be a valid question.

Anon
+1 best answer so far.
aioobe
+4  A: 

Define some basic multi-threaded primatives.
Do this so they are generic and not platform specific.

Then ask hom to write a higher level multi-threadec construct.

Example:

class Lock
{
    void lock();       // Thread safe acquisition of lock.
                       // Same thread may lock multiple times.
                       // Each call to lock() requires a call to unlock()
    void unlock();
};
class ConditionVariable
{
    wait(Lock& lock);  // Calling thread is suspended and added to wait queue.
                       // Lock is released.
    signal();          // Releases one thread in the wait queue.
                       // This means that a thread that has previously called wait()
                       // will be un-suspended and allowed to exit the wait() method.
                       // Note: The thread will have re-quired the lock before exiting wait()
};

Question 0:

Describe what you know about locks and condition variables and comment on the above generic descriptions.

Question 1:

Build an exception safe lock.

After he has finished get him/her to explain how it works and why it is exception safe.

Question 2:

Build an exception safe semaphore

After he has finished get him/her to explain how it works and why it is exception safe.

Question 3:

Build a thread pool.

After he has finished get him/her to explain how it works. Get them to show you several uses cases and explain how it would be used.

Question 4:

Explain a real world problem that you had with threads last year.

Get them to make suggestions about the problem (see if he gives you any new ideas) :-)

Martin York
Hard questions. >_<. I would assume that you just want to check for understanding, as opposed to an actually working implementation? BTW: question #2 is kinda different from whta I'd expect... didn't Dijkstra create Mutexes and Condition Variables *FROM* semaphores?
Dragontamer5788
@Dragontamer578: To me a semaphore is something that will let 'n' threads into an area of memory. You can consider it a mutex with a count. On this Wiki page they are referred to as `counting semaphores` http://en.wikipedia.org/wiki/Semaphore_%28programming%29
Martin York
A: 

People who think software development skills and experience can be measured in terms of mundane tasks such as sorting, searching, etc are simply clueless.

If/when a member in my team needs to implement some low level algorithm that's already been solved and documented profoundly in the literature, I urge him to pick his data structures book and double check his implementation even if he claims he knows it by heart. Why take the risk? Understanding business requirements, communication skills, being a team player, having solid education and experience, projects completed and shipped, making best use of tools... these matter the most for software development. I don't care how fast he can type from memory.

Understanding multi-threading without any experience is nonsense. Make sure he can show real multi-threaded work that's in production and does not deadlock and makes efficient use of system resources.

ifyes
+2  A: 

What is Amdahl's Law?

Anyone who claims to be expert in multi-threading must be aware of Amdahl's Law.

Design a read-write lock that allows multiple readers to read the resource but allows only a single writer to modify the resource. A writer cannot modify resource when one or more readers are accessing the resource and vice-versa. How do you provide fairness to writers when large number of readers exist compared to number of writers?

LionHeart