Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample Problems for Multithreading Practice

I'm about to tackle what I see as a hard problem, I think. I need to multi-thread a pipeline of producers and consumers.

So I want to start small. What are some practice problems, in varying levels of difficulty, that would be good for multi-threading practice? (And not contrived, impractical examples you see in books not dedicated to concurrency).

What books or references would you recommend that focus on concurrency and give in-depth problems and cases?

(I'd rather not focus on the problem I want to solve. I just want to ask for good references and sample problems. This would be more useful to other users. I'm not stuck on the problem.)

like image 385
moogs Avatar asked Jan 15 '09 10:01

moogs


People also ask

What is multithreading real life example?

Real-life ExampleSuppose you are using two tasks at a time on the computer, be it using Microsoft Word and listening to music. These two tasks are called processes. So you start typing in Word and at the same time start music app, this is called multitasking.

What are some examples of multithreaded applications?

Another example of a multithreaded program that we are all familiar with is a word processor. While you are typing, multiple threads are used to display your document, asynchronously check the spelling and grammar of your document, generate a PDF version of the document.

What problems does multithreading solve?

Multithreaded programs allow 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. Threads improve the application performance using parallelism. They share information like data segment, code segment files etc.


3 Answers

The little book of semaphores is a good free book. The author takes a unique approach of first asking a problem and then presenting hints before answering. The problems increase in difficulty level gradually, and the book isn't written for any language in particular but covers general multithreading concepts.

like image 164
Raminder Avatar answered Nov 11 '22 11:11

Raminder


If you have enough time to invest I would recommend the book "Concurrency: State Models & Java Programs, 2nd Edition" by Jeff Magee and Jeff Kramer, John Wiley&Sons 2006

You can ignore the Java part if you are using some other language

There's a language used to model processes and concurrent processes called FSP. It needs some time and energy to be invested in order to be proficient in the language. There's a tool (LTSA, both are free and supported by an Eclipse plugin or stand alone app) which verifies your models and make you pretty shure that your model is correct from the standpoint of concurrent execution.

Translating this models to your language constructs is then just a question of programming technique and few design patterns.

like image 29
Boris Pavlović Avatar answered Nov 11 '22 12:11

Boris Pavlović


Most text book problems, like readers-writers, producers-consumers or dinning philosophers are all illustrations of the mutex. I would prefer to model a prototype which is a simplistic approximation the bigger problem and go ahead.

I have some times seen situations where dead-lock avoidance is what is needed and dead-lock prevention measures are being used. It is always a good idea to analyse if Banker's algorithm would suit the case or not.

like image 27
questzen Avatar answered Nov 11 '22 11:11

questzen