Concepts / Concurrent Programming with Threads and Locks

Concurrent Programming with Threads and Locks

The source for this article names concurrent programming as a category and describes its purpose as running work at the same time.

  • Programming

Why the Library Arrives in Groups

The C++ standard library is large. It offers facilities for memory allocation, text handling, input and output, data collections, mathematics, concurrency, and more. Meeting this collection for the first time can be overwhelming, because the individual names do not explain how they relate to one another. Seeing a list of names gives you no sense of which ones matter together or which ones are needed before others.

The standard library addresses this in its own documentation by classifying its facilities into groups. Once you know the groups, a new facility has a place in your mental map, and you can predict roughly what it is for before you read its details. This article follows that classification. The goal is not to memorize every facility. The goal is to build a picture of the whole library that you can return to as you learn each part in detail.

Before reading the groups, pause and try a short exercise. Imagine you are designing a library that programs could build on. What kinds of help would you expect it to offer? Keep your answer in mind, then compare it with the categories described below. You will notice that many of your expectations appear in the list, and a few may surprise you.

The Eleven Groups at a Glance

The source describes the standard library as a set of facilities that can be classified into eleven groups. They are run-time language support, the C standard library, strings, regular expression matching, I/O streams, containers and algorithms, numerical computation, concurrent programming, template metaprogramming, smart pointers, and special-purpose containers. Each group answers a different kind of need. Some groups support the language machinery underneath every program, some support handling text and data, and some support building larger or more general code.

C++ StandardLibrarya set of classifiedfacilitiesRun-time languagesupportallocation, run-time typeinformationC standard libraryolder C facilities, adaptedStringstext handlingRegular expressionmatchingfinding patterns in textI/O streamsinput and outputContainers andalgorithmsorganizing and processingdataNumerical computationmathematicsConcurrentprogrammingwork running at the sametimeTemplatemetaprogrammingcode that works across manytypesSmart pointersmanaging resourcescorrectlySpecial-purposecontainersarray, bitset, tupleincludesincludesincludesincludesincludesincludesincludesincludesincludesincludesincludes
Which categories make up the standard library, and which representative facilities sit inside each one? The groups are siblings under one library, not stacked on top of one another.

Read the diagram as a map, not as a stack. Each group is a separate kind of support that the library offers. The groups are not layered so that one must sit on top of another, so you should not infer a required order of use from the picture. The grouping is useful because it tells you where to look when you need a kind of help.

The Foundation: Language Support and the C Library

The first two categories form the foundation on which the rest of the library rests. The first is run-time language support. It provides facilities for allocation and for run-time type information. Allocation is the act of obtaining memory while a program runs. Run-time type information lets a program inspect type information while it executes. These are facilities the language needs in order to operate, which is why they are grouped first. You rarely make them the main topic of a program, but many other facilities depend on them.

The second foundational category is the C standard library. The source states that it is included in the C++ standard library with very minor modifications, and those modifications exist to minimize violations of the type system. This tells you something about the design. The C++ library did not discard the older C facilities. It adapted them so that they fit the stricter type rules of C++ as closely as possible.

Text, Patterns, and Data Movement

Many programs spend much of their time working with text, so the standard library groups three related kinds of support together. These categories are all about how a program reads, represents, and searches character data. They are presented separately because each one answers a different question. Strings answer how text is stored and handled. Regular expression matching answers how patterns are found inside text. I/O streams answer how data moves in and out of a program.

Keeping these three apart makes classification easier. If your question is about the shape of a piece of text, you are looking at strings. If your question is about finding a pattern such as a particular format inside text, you are looking at regular expression matching. If your question is about reading from or writing to the outside world, you are looking at I/O streams.

Organizing and Computing with Data

The next two categories cover organizing data and computing with it. Containers and algorithms, which the source identifies with the STL, handle data organization. Numerical computation covers mathematical work. Both categories are large and important, and both are used constantly, which is why they are often the first parts of the library that programmers learn in depth.

Containers and algorithms are often used together. A program might hold a collection of values in a container and then process those values with an algorithm. The classification keeps these two roles distinct: the container is where the data lives, and the algorithm is what is done with it.

Advanced Concerns: Concurrency, Generics, and Resources

The last three categories in the classification address more advanced concerns. Concurrent programming addresses running work at the same time. Template metaprogramming addresses writing code that works across many types. Smart pointers address managing resources so that they are handled correctly. Each is a distinct category with its own purpose, and none of them is simply a variation of another.

Special-Purpose Containers

The final category is special-purpose containers. The source lists three: array, bitset, and tuple. All three are containers, but the word special-purpose signals that each serves a narrower role than the general containers of the STL. Treat them as a set of distinct tools rather than one idea. Knowing that they are containers tells you they hold data. Knowing that they are special-purpose tells you that their role is narrower than the general containers.

Language Support versus Everyday Tasks

Two broad roles appear across the eleven groups. Some facilities exist so the language can operate at all. Others exist so that programmers can do ordinary work. The table below sets these two roles side by side, so you can see how the groups divide.

AspectLanguage supportEveryday programming tasks
GroupsRun-time language support and the C standard libraryStrings, regular expression matching, I/O streams, containers and algorithms, numerical computation
Representative facilitiesAllocation and run-time type informationText handling, pattern matching, input and output, data collections, mathematics
Role in a programNeeded for the language to operateUsed directly to read, store, search, and process data
How often you call them directlyRarely, as a main topicOften, and early in learning the library
Relationship to other facilitiesMany other facilities depend on themOften combined with one another in one program

Foundation facilities versus facilities for everyday tasks

Tracing a Program Across Groups

Groups are not exclusive. A program may use facilities from several groups at once. Consider a simple task in which a program reads text, keeps it, and then works on it. Each step can be matched to a group, and the flow runs from left to right.

I/O streamsread text inContainershold the textAlgorithmsprocess the datapasses data topasses data to
Which groups does a single task draw on, and in what order does the data move? The classification tells you where to look for each step, not which single group the program must use.
Worked example

Classifying an unfamiliar facility by its purpose

You meet a facility whose purpose is to hold a resource and release it correctly when it is no longer needed, so that the program does not forget to clean up. Which category does it belong to?

  1. 1
    Name the problem

    The facility manages a resource and ensures it is handled correctly. The source's own wording for this purpose is managing resources so that they are handled correctly.

  2. 2
    Match the purpose to a category

    The source lists smart pointers as the category for managing resources. Concurrency, generics, and the foundation groups address other purposes.

  3. 3
    Check against the name

    The name of the facility might not reveal its category. The method is to rely on purpose, not on the name alone.

Result

The facility belongs to the smart pointers category.

Mistakes When Classifying Facilities

Beginners tend to make a few predictable errors when they use the classification. Each one is easy to correct once you know what to look for.

  • Common mistake

    Classifying a facility by its name alone

    What it looks like

    Assuming a facility belongs to a group because its name sounds similar to the group's name.

    Why it fails

    The source says to identify purpose and match it to the category rather than relying on the name alone.

    Do this instead

    Ask what problem the facility solves, then match that problem to the category.

  • Common mistake

    Assuming a program must use exactly one group

    What it looks like

    Believing that a text-processing program uses only the strings group.

    Why it fails

    A program may use facilities from several groups at once, such as I/O streams, containers, and algorithms together.

    Do this instead

    Treat the classification as a guide to where to look, not as a rule about which single group to choose.

  • Common mistake

    Treating the foundation groups as optional

    What it looks like

    Ignoring allocation because you never call it directly.

    Why it fails

    Many other facilities depend on the foundation groups, even when you do not name them in your code.

    Do this instead

    Recognize that foundation facilities support the language underneath everything else.

  • Common mistake

    Reading the groups as a stack that must be learned bottom to top

Practice: Place a New Facility

EASY

A colleague describes a facility whose purpose is to let the same kind of logic work across many different types, so that one piece of code can be written once and reused for each type. Using only the classification in this article, decide which category it belongs to and explain your reasoning by naming the purpose you matched.

Hints
  • Focus on the purpose the colleague describes, not on any name they mention.
  • Compare the purpose with the three advanced concerns: concurrency, generic code across types, and resource management.
  • The category you need is the one whose purpose is writing code that works across many types.

Mapping the Library Before the Details

The classification of the library into groups gives you a map before you learn each part in detail. When you meet a new facility, place its purpose on the map first, then move to its details. The map is what lets you predict what a facility is for before you read about it.

Key Takeaways

  • The standard library is organized into eleven groups so that a large collection becomes a map you can navigate.
  • Run-time language support and the C standard library form the foundation, providing allocation, run-time type information, and adapted older C facilities.
  • Strings, regular expression matching, and I/O streams support text and data movement; containers and algorithms and numerical computation support data organization and computing.
  • Concurrent programming, template metaprogramming, and smart pointers address advanced concerns, and special-purpose containers (array, bitset, tuple) serve narrower roles.
  • To classify a facility, identify its purpose and match it to a category rather than relying on its name, and remember that one program may draw on several groups.

Related concepts

More concepts starting with C