views:

170

answers:

11

Hi,

If I want to find truly experience the pain of writing multi-threaded applications, What program should I try writing?

Note, I would like to know of an example that is not too big and serves as a good example of demonstrating the intricacies for teaching purposes.

thanks

+5  A: 

A server that can handle multiple client connections and pass data between them. Could be something simple like a chat server.

Rob Curtis
+1 was going to suggest the same example.
Greg Hewgill
Was thinking the same thing.
Sapph
No, no, noooo. Not yet another chat server! There must be at least a million of these things. Do something a little bit more original, please.
anon
@Neil: They're not at all original, but they are a good learning experience. Personally, I feel every dev. should write at least one chat server.
Reed Copsey
+1  A: 

I recommend you write a POS application (No, not piece of sh*t). There are many devices you have to talk to and read data from (receipt printers, swipe readers, fee displays, etc) that it will all have to synchronize with each other. Once you are able to tackle that, you can pretty much tackle 90 percent of most multi-threaded applications. Don't take the easy route and use Timers as my predecessor did and now I am in the process of re-writing the whole application to use threads!

0A0D
+1  A: 

Depends on your goal I think, but two examples that come to mind:

  • For multithreading, consider writing a multithreaded file copy application. You will need to copy each file using a different thread; in fact, this is a good example for which you could use a threadpool, depending on the platform/language you're using
  • For concurrency, the classic example is the ATM. Try writing some kind of ATM emulator or application where users could be accessing their account and changing information from multiple locations. You will need to deal with locking and things of that nature on the server side.
Ed Altorfer
File copying typically does not scale well, so for learning, I feel it's a bad idea. Typically, you're hardware IO bound when copying files, so running in multiple threads will slow down your program. This makes it tough to see what's happening when you multithread...
Reed Copsey
Ever used single-threaded file copy? :) In part, you're accurate, but there's a definite impact for this scenario.
Ed Altorfer
A: 
JBrooks
A: 

Create your own text editor that can open/edit/save more than one file at a time.

ChadNC
How does this require multithreading?
Reed Copsey
threading yes for speed, syncing not compulsory.
ajay
Yea, really don't see this as multi-threaded unless you are talking about some sort of behind the scenes auto-save feature, but that's pretty much it. Kinda boring!
0A0D
There are many reasons why a text editor could and should be multithreaded and it's not hard to figure out what those reasons are if you have ever attempted to create a fully functional text editor like textpad on your own.
ChadNC
+2  A: 

I've just finished writing a Multi-User Dungeon server for a networking class and it was very fun. I've learned a lot about concurrency doing it. There's communication, movement through rooms, battle, interaction with rooms... You can decide yourself how complicated you want to make it.

nagnatron
sounds good! how long did it take to write?
ajay
I just checked my svn log and it took me about two weeks but I didn't work full time on it. But really, how much it will take you, mostly depends on what features you want to implement.
nagnatron
A: 

If you are looking for a fun concurrent processing example to try and wrap your head around and attempt to solve, I would recommed the famous dining philosophers problem.

Wikipedia Entry -- Dining Philosophers Problem

George
A: 

A Tablet PC application..... In particular, RealTimeStylus. It can get pretty hairy!

Josh Einstein
A: 

Have you ever heard of the game spoons? I learnt all my multithreading and concurrency from there. Try to write a game where you play against the computer and the computer plays intelligently, etc.

Otherwise, a server is also 'fun'.

http://en.wikipedia.org/wiki/Spoons

piggles
A: 

There's a basic problem with picking one example to "truly experience the pain". That problem is that threading is used for quite a few things that are (almost) completely different from each other.

  1. Maintain a UI in one thread and compute in another
  2. Split large computation across many processors
  3. Simulations of discrete entities, each acting independently of others
  4. Decoupling code that deals with devices that work at different speeds

Technically, #1 is a special case of #4, but it's so common, and has enough (nearly) unique requirements, that it's generally easiest to keep it separate.

Of course, that list isn't exhaustive, but I hope it gives some idea of the situation. The idea it should give is that different uses of threading have substantially different concerns, and the "pain" isn't the same for all of them.

Performance concerns, for one example, vary widely across those applications. In some cases virtually all of your threads will nearly always be idle, and there's virtually no contention when you need to synchronize. In other cases, you'll nearly always have lots of threads active, with far more contention over synchronization.

Jerry Coffin
A: 

Try a "hand-over-hand" or "chain locking": you acquire the lock of node A, then node B, then release A and acquire C, then release B and acquire D and so on

Loop