A Multi-threading Library for Standard C++

Document number:   N1682=04-0122
Date:   September 10, 2004
Project:   Programming Language C++
Reference:   ISO/IEC IS 14882:2003(E)
Reply to:   Pete Becker
  Dinkumware, Ltd.
  petebecker@acm.org


Introduction · Design Overview · Implementation Experience


Introduction

One of the most common complaints about the C and C++ standards is that they do not provide support for writing portable multi-threaded applications. While most programmers should not be writing multi-threaded code, with proper high-level design a set of low-level library primitives can greatly enhance the portability of multi-threaded code. This paper proposes the addition of such a library to the C++ standard, either in the upcoming Library Technical Report or in the next revision of the C++ standard.

Design Overview

The interface to the multi-threading library is described at the Dinkumware web site. If that link doesn't take you directly to the documentation, click on the icon with the footprints. The documentation describes both a C++ interface and a C interface to the multi-threading library. We propose only the C++ interface. If the committee decides to proceed along these lines we will make our documentation available for standardization purposes.

The library provides half a dozen library primitives to support multi-threaded programming. Most of these primitives should be familiar to anyone with experience in POSIX or Java. The primitives are: threads, thread groups, once functions, condition variables, mutexes, and thread-specific storage.

Threads

The class thread can be used to create a new thread, join an existing thread, and suspend execution of a thread for a period of time.

Thread Groups

An object of type thread_group can be used to manage multiple threads. It can create a new thread that belongs to the group, it can add threads to and remove threads from the group, and it can be used to wait until all the threads in the group have terminated.

Once Functions

The function call_once, along with its supporting type once_flag, can be used to insure that an initialization function is called only once, even if multiple threads require the same initialization.

Condition Variables

An object of type condition can be used to block execution of threads until another thread indicates that they can proceed. This typically means that the controlling thread has generated data that is needed by the blocked threads.

Mutexes

Objects of the various mutex types can be used to insure serial execution of critical sections of code used by multiple threads.

Thread-specific Storage

An object of type thread_specific_ptr can hold a different pointer value for each thread that accesses it.

Implementation Experience

The Dinkumware implementation of this thread library has been in use by customers for two years without significant problems. The interface is based on the boost.threads library, which has also been in wide use for several years.

The underlying C code is based on code in the Dinkum C Library. This code is widely used on many platforms. It is highly portable and robust.