1

I got an interview for a developer internship position, and they're making me write a test tomorrow (got the interview offer today, so it's kind of a short notice). Bear with me and my n00bish questions, but I haven't done C++ in around 4 months and I had to study for two midterms, so I don't have a large amount of time to look this up myself.

Essentially, I'm wondering what deadlock and race-conditions are in C++ are, and what are some simple and intermediate-level examples of this.

Thanks for your help! :)

Jay
  • 357
  • 3
  • 8
  • 18
  • possible duplicate of [What is a race condition?](http://stackoverflow.com/questions/34510/what-is-a-race-condition) – Gian May 29 '13 at 05:03
  • 1
    google will give better results for this. you might get ready answers than waiting here for detailed answers. – Koushik Shetty May 29 '13 at 05:04
  • These terms are not going to be more meaningfully applied to C++ than to any other language that supports some kind of concurrency. The generic definitions can be applied directly to C++. – Gian May 29 '13 at 05:04
  • This is more of a general question than a c++ question. Deadlocks and race conditions occur with threads throughout computing. – Matt May 29 '13 at 05:05
  • Try this question, as well: http://stackoverflow.com/questions/3130079/difference-between-racearound-condition-and-deadlock As phrased here, this isn't really an ideal question for the stack overflow format. – Don McCurdy May 29 '13 at 05:06

2 Answers2

6

The Java Tutorials have a good anecdote that explains deadlocks:

"Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Here's an example. Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time."

And this answer addresses race conditions.

Community
  • 1
  • 1
isaach1000
  • 1,819
  • 1
  • 13
  • 18
  • I think this is a very bad explanation of deadlock, and as the interviewer I would ask for another example. Now I might be wrong, but I still would ask for another example and if you don't have one, then you don't know what a deadlock is. – gnasher729 Mar 30 '14 at 16:55
1

If you're more specific on which platform, then we can give more explicit examples. e.g. if it's Windows, then a making your process multi-threaded (e.g. CreateThread) and create scenarios whereby each thread are competing for resources that might block (CreateEvent & WaitForSingleObject) also SendMessage.

Stephen Quan
  • 21,481
  • 4
  • 88
  • 75