Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for a good exercise to help me get better at Multithreading

I think of myself as a pretty decent developer, however when it comes to multithreading I'm a total n00b. I mean the only multithreading I've done at work was the very basic stuff like spawning off multiple threads using the ThreadPool to do some background work. No synchronization was necessary, and there was never any need to create threads manually.

So, my question is this; I want to write some application that will need to be heavily multithreaded and that will need to do all of the advanced things like synchronization etc.. I just can't think of anything to write. I've thought of maybe trying to write my own ThreadPool, but I think I need to learn to walk before I can run. So what ideas can anyone suggest? It doesn't have to have any real world useage, it can be totally pointless and worthless, but I just want to get better. I've read tons of articles and tutorials on all the theory, but the only way to REALLY get better is by doing. So, any ideas?

like image 547
BFree Avatar asked Feb 01 '10 15:02

BFree


People also ask

How can we achieve multi threading?

We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface. Using runnable will give you an object that can be shared amongst multiple threads.

How multithreading can improve performance of the program?

Multithreading decreases performance in the sense that it increases the total CPU time required to perform a task. However, it increases performance in the sense that it (usually, and depending on the task's characteristics) reduces the wall clock time that is required to perform the task.

What is the benefit of going for multi threading?

Multithreading allows the execution of multiple parts of a program at the same time. These parts are known as threads and are lightweight processes available within the process. So multithreading leads to maximum utilization of the CPU by multitasking.


3 Answers

  1. Recursive Quick Sort. Compare sort time as a function of number of threads.
  2. Craps simulator. How many rolls of the dice can you do per minute?
  3. Web page crawler. Give it a URL and download all of the child pages and images. Watch for pages that reference each other so you don't go into an infinite loop. Note that the threads will block waiting for the network response, giving you a different CPU utilization than pure calculation-based threads. Use a queue to keep track of unread pages and a dictionary to keep track of active threads. Threads that timeout go back to the queue.
  4. WCF web server. Spawn a new thread for each request. Write a multi-threaded WPF client that updates the user-interface in real-time.

Is that enough?

like image 167
Ed Power Avatar answered Oct 23 '22 16:10

Ed Power


How about some kind of pointless batch-processing app? Generate a horrendous amount of data in a single thread and dump it out to a file, then start splitting the work off into threads of differing sizes and dumping them out to another file, time them and compare the files at the end to ensure the order is the same. This would get you into multi-threading, locking, mutexes and what not and also show the benefits of multithreading certain tasks vs. processing in a single thread.

First thing that popped into my head. Could be dull and/or pointless, but don't shoot the messenger! :)

like image 28
Antony Koch Avatar answered Oct 23 '22 17:10

Antony Koch


I think you should draw attention for this books:

  1. Windows via C/C++ by Jeffrey Richter. This is one of the best books about multithreading
  2. Concurrent Programming on Windows by Joe Duffy. Another excelent book about multithreading

Excelent articles by Herb Sutter (Herb, we all waiting for your new book!)

Effective Concurrency series

Some blogs:

  1. Herb Sutter's blog.
  2. Parallel programming with .Net
  3. Jeffrey Richter's Blog
  4. Joe Duffy's blog

P.S. How about Power Threading as an example of multithreading (and ThreadPool implementation)?

like image 33
Sergey Teplyakov Avatar answered Oct 23 '22 17:10

Sergey Teplyakov