Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand C++ multithreaded programs

Shulou Source: shulou.com Published: 2022-06-01 19:48:36 09月29日 Update

This article mainly explains "how to understand C++ multithreaded programs", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to understand C++ multithreaded programs.

CP.1: imagine your code becoming part of a multithreaded program

Reason (reason)

It is difficult to determine that concurrency is not needed now or will not be used at some point in the future. The code will be reused. Libraries that do not use threads may be used by other parts of the program that use threads. Note that this guideline has the greatest urgency for functional libraries, but not for individual applications. However, over time, code snippets can appear in unexpected places.

Example, bad (negative example)

Double cached_computation (double x)

{

/ / bad: these statics cause data races in multi-threaded usage

Static double cached_x = 0.0

Static double cached_result = COMPUTATION_OF_ZERO

If (cached_x! = x) {

Cached_x = x

Cached_result = computation (x)

}

Return cached_result

}

Although cached_computation works perfectly in a single-threaded environment, in a multi-threaded environment the two static variables result in data races and thus undefined behavior.

Although cached computing can run perfectly in a single-threaded environment, two static variables compete for data in a multithreaded environment, resulting in undefined behavior.

Example, good (sample)

Struct ComputationCache {double cached_x = 0; double cached_result = COMPUTATION_OF_ZERO

Double compute (double x) {if (cached_x! = x) {cached_x = x; cached_result = computation (x);} return cached_result;}}

In this code, the cache data is stored as a data member of the ComputationCache object, rather than static state data. This refactoring fundamentally delegates the decision up to the caller: single-threaded programs can continue to use global ComputationCache instances, while multithreaded programs can manage one ComputationCache instance per thread, or one instance per context, which can be interpreted in many ways. The refactored function no longer attempts to manage the memory allocation of cached_x. From this point of view, this is an application of the principle of separate responsibility.

In this particular case, the goal is thread-safe refactoring while improving reusability in a single-threaded environment. It's not hard to imagine that a single thread might need two ComputationCache instances for different parts of the program without overwriting the cached data.

There are many other ways, one of which is to add thread-safe processing to code running in a standard multithreaded environment (that is, using the only form of concurrency std::thread).

Mark the state variables as thread_local instead of static.

Define a state variable as a local variable within a thread rather than a static variable.

Implement concurrency control, for example, protecting access to the two static variables with a static std::mutex.

Implement concurrency control, for example, using static std::mutex to protect access to two static variables.

Refuse to build and/or run in a multi-threaded environment.

Refuses to compile or execute code in a multitasking environment.

Provide two implementations: one for single-threaded environments and another for multi-threaded environments.

Two implementations are provided: one for a single-threaded environment and the other for a multithreaded environment.

Exception (exception)

Code that is never run in a multi-threaded environment.

Code that never runs in a multithreaded environment.

Be careful: there are many cases where code that would never be run in a multithreaded program ends up as part of a multithreaded program, usually a few years later. Generally speaking, it is very painful to eliminate data competition for such a program. Therefore, once it is decided that the code will never run in a multithreaded environment, it should be clearly marked, and it is best to provide both compilation and runtime enforcement mechanisms to catch incorrect usage as soon as possible.

At this point, I believe that everyone on the "C++ multithreaded program how to understand" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Threads programs code environment data runs variables static instances caching refactoring security urgency up and down context content and at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Microsoft Shulou Information Xiaomi