Skip to content
Francisco Ruiz edited this page Nov 2, 2015 · 18 revisions

Introduction

Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer.

The main target of these sessions is to spread some knowledge about techniques and concepts used in multithreaded programming.

This small course will be composed of five practical sessions when we speak about how to improve or solve some issues for a source code written by using new features of c++11.

If you think you do not have enough knowledge to start these sessions, don't worry!, there are millions of pages on internet how speaks about multithreading, you can take a quick look to Simpler Multithreading in C++0x and Youtube channel about concurrency. And remember you should read the book C++ Concurrency in Action: Practical Multithreading

After reading and seeing these two pages, perhaps you do not need to do these sessions, but you can come and share your knowledge with all of us.

After these five sessions we hope you will know:

  1. Life cycle of threads.
  2. Techniques to avoid dead locks.
  3. Techniques to synchronize threads efficiently.
  4. How to using helgrind and DRD.

General Linux environment

All source code of these sessions has been developed using ELX (Ericsson distribution of Ubuntu 14.04) on a quad-core with 8 Gigabytes of memory and running g++ version 4.8 (there is some functionality available since g++ version 4.6 but it is not complete).

you should install package libboost-all-dev.

To generate executable files you will use make tool.

You need to install valgrind package and tools helgrind and DRD which will be very useful to study what is happening during our multithread execution.

You could compile the problems and solutions going to its directory and running Makefile.

Study what it is happening in our multithreaded programs.

To study problems related with multithreaded programming we count with two useful tools which run under valgrind ecosystem.

Helgrind and DRD store information related to multithread execution and try to show the list of detected problems. What is better? No one, it depends what kind of problem we are studying. You should run the two of them to know what is better for finding out your problem.

To run this code by using these tools you must run the next two commands:

valgrind -v --tool=helgrind ./dirty

valgrind -v --tool=drd ./dirty

These commands will show you the list of problems related to multithreading detected during the execution of the program.

Both tools can detect hundreds of false errors related with STL, boost and other system libraries. You will need one cleaner report about your execution to be able to solve problems. This guide will tell you how to create the file to configure every tool to ignore problems that are not important for us.

You have helgrind.supp and drd.supp which contain pre-generated filters to avoid too much noise around our issue.

You should generate your own suppression file because it is very specific for your OS. Run next commands to generate:

valgrind --tool=helgrind --gen-suppressions=all --log-file=temporary ./dirty

cat temporary | ../../common/suppression.sh > helgrind.supp

You can read a more detailed information in https://wiki.wxwidgets.org/Valgrind_Suppression_File_Howto

Run this code again, but this time you must indicate the suppression file:

valgrind -v --tool=helgrind --suppressions=helgrind.supp ./dirty

valgrind -v --tool=drd --suppressions=drd.supp ./dirty

As you can see our problem now it is much cleaner.

Let's have some fun.

First session - Do not lock/unlock mutex

Second session - Logical layers

Third session - Resource acquisition order

Fourth session - Synchronizing threads

Fifth session - Sequential vs ways of parallelization