8. DRD: a thread error detector
To use this tool, you must specify
--tool=drd
on the Valgrind command line.
8.1. Overview
DRD is a Valgrind tool for detecting errors in multithreaded C and C++ programs. The tool works for any program that
uses the POSIX threading primitives or that uses threading concepts built on top of the POSIX threading primitives.
8.1.1. Multithreaded Programming Paradigms
There are two possible reasons for using multithreading in a program:
• To model concurrent activities. Assigning one thread to each activity can be a great simplification compared to
multiplexing the states of multiple activities in a single thread. This is why most server software and embedded
software is multithreaded.
• To use multiple CPU cores simultaneously for speeding up computations. This is why many High Performance
Computing (HPC) applications are multithreaded.
Multithreaded programs can use one or more of the following programming paradigms. Which paradigm is appropriate
depends e.g. on the application type. Some examples of multithreaded programming paradigms are:
• Locking. Data that is shared over threads is protected from concurrent accesses via locking. E.g. the POSIX threads
library, the Qt library and the Boost.Thread library support this paradigm directly.
• Message passing. No data is shared between threads, but threads exchange data by passing messages to each other.
Examples of implementations of the message passing paradigm are MPI and CORBA.
• Automatic parallelization. A compiler converts a sequential program into a multithreaded program. The original
program may or may not contain parallelization hints. One example of such parallelization hints is the OpenMP
standard. In this standard a set of directives are defined which tell a compiler how to parallelize a C, C++ or Fortran
program. OpenMP is well suited for computational intensive applications. As an example, an open source image
processing software package is using OpenMP to maximize performance on systems with multiple CPU cores. GCC
supports the OpenMP standard from version 4.2.0 on.
• Software Transactional Memory (STM). Any data that is shared between threads is updated via transactions. After
each transaction it is verified whether there were any conflicting transactions. If there were conflicts, the transaction
is aborted, otherwise it is committed. This is a so-called optimistic approach. There is a prototype of the Intel C++
Compiler available that supports STM. Research about the addition of STM support to GCC is ongoing.
DRD supports any combination of multithreaded programming paradigms as long as the implementation of these
paradigms is based on the POSIX threads primitives. DRD however does not support programs that use e.g. Linux’
futexes directly. Attempts to analyze such programs with DRD will cause DRD to report many false positives.
8.1.2. POSIX Threads Programming Model
POSIX threads, also known as Pthreads, is the most widely available threading library on Unix systems.
The POSIX threads programming model is based on the following abstractions:
121