C++ Builder - MultiThreading
I have a bit of a problem. I teach C++ Builder for the first time this year and I need to create an assignment for my students regarding multi-threading. I would use TIdThreadComponent or TThread. What I would like them to do is to create multiple threads (for some reason), then to upgrade it by using critical sections. I will also include semaphores (mutex) but that part in not a problem.
So, my problem is not the knowledge on how to do programming but on how to give a quality assignment that would make some sense and what would be a good example on how and when to use multi-threading and critical sections. My inspirations is really low at the moment so please help...
It would be good for the students to understand when to use threads, and when not. Ive seen many questions here where people think that making something multi-threaded will "automatically" make the application faster, which is a huge misunderstanding of threads.
So, show them 2 types of problems: one that can be solved with threads, and the other that should not be solved with threads.
Should not be solved with threading:
Work that can not be divided into smaller tasks and/or can not be performed in parallel. For example: number calculations that cant be broken down into sub-tasks, or serial file manipulation.
Can be solved with threading:
A typical example would be socket communication. A single threaded server most likely wont be as performant as a multi-threaded server. Each message can be a smaller task that can be enqueued and given to a thread pool.
If they are supposed to know Monte-Carlo simulations, - write some some code which will simulate a brownian motion and estimate the probability of it being above a certain price. - write a multithreaded app, which would use cores to estimate the patch. - measure the improvement in time
Now you need to find an assignnment where concurrency could be an issue.
You could give them an app that updates a variable in a simulation of real time, and they will have to modify the code to process the data using all cores. The data should change such as if they do not use the critical sections properly, it will generate gibberish output ?