C++ Concurrency in Action 2nd Edition

Chinese translation of “C++ Concurrency in Action – SECOND EDITION” – mianshigee.com (mianshigee.com)

C/C++ learning tutorial source code-C/C++ source code recommendation-Interview Brother (mianshigee.com)

The author is the C++ standard committee member who introduced the thread library for the C++11 standard! And the author of this book has also written numerous multi-threading and concurrency-related proposals that constitute the C++ standard, formulated the Concurrency Technical Specification (Concurrency Technical Specification), and about the future of C++ Proposal for evolutionary development.

“C++ Concurrent Programming in Practice 2nd Edition” is the culmination of the author’s years of hard work. This book is based on all of his experience studying C++ and multithreading, and is intended to guide other C++ developers in the safe and efficient use of the C++ thread library and concurrency technical specifications. It can be said that if you follow this book to learn C++ concurrent programming, you are learning according to the standards. The authoritativeness of this book is beyond doubt!

Attentive students must have noticed that what Renyoujun recommends this time is the latest published 2nd edition. This is because the first edition of “C++ Concurrent Programming in Practice” was based on C++11, but later the C++ Committee successively released the C++14 standard, C++17 standard and some technical specifications. , further provides support for writing multi-threaded programs. For example, C++17 can use multi-threads or multi-processors, allowing performance-sensitive tasks such as image processing and machine learning to be completed more quickly. The “C++ Concurrent Programming Practical 2nd Edition”covers the new features of modern C++, especiallyfor industrial-grade C++ Concurrency unlocks features, patterns, and best practices.

  • Starting from the various tools of the C++ standard library, it introduces thread management, sharing data between threads, synchronization of concurrent operations, C++ memory model and atomic operations.
  • Rich and comprehensive content, introducing lock-based concurrent data structures, lock-free data structures, concurrent code, as well as advanced thread management, parallel algorithm functions, and testing and debugging of multi-threaded applications.
  • Updated and revised according to the C++14 and C++17 standards, covering the latest changes in all standards. This second edition shows readers how to write elegant and robust multithreaded applications in C++17, telling you all the details.
  • ● Provides a wealth of supplementary information through appendices and online resources to help readers grasp the knowledge of C++ concurrent programming more completely and in detail.

Not only is the knowledge structure complete, this book also specifically echoes the theme of “In Action”, examples and exercises are provided in each chapter, and the author of this book – the C++ Standards Committee Member and concurrent programming guruAnthony Williams has some unique insights, which are very valuable for developers.

The examples given in this book are concise and representative, such as:

  • ● Designing lock-based concurrent data structure (Designing lock-based concurrent data structure)
  • ● Designing lock-free concurrent data structure (Designing lock-free concurrent data structure)
  • ● Designing concurrent code
  • ● Advanced thread management
  • ● Parallel algorithm
  • ● Testing and debugging multithreaded application

Some of the codes in these examples can even be directly copied to a production environment. I believe that after reading them carefully and practicing them, you will gain some valuable experience in multi-threaded programming from the author and have a deeper understanding of the underlying layers. Know.

The translator of this book communicated with the author through a large number of emails and worked on it repeatedly to ensure that the translation of the whole book was accurate, concise and easy to understand. In addition, the translator also supplemented many extended knowledge points based on his own development experience, and provided readers with nearly 200 pages of electronic appendix strong>D and more than 140 supporting code files. It directly eliminates the readers’ worries about the translated books being acclimatized! (Really? People post hey, hehe!)

https://github.com/xiaoweiChen/CPP-Concurrency-In-Action-2ed-2019

C++ those things

GitHub – Light-City/CPlusPlusThings: C++Things

Thank you for your support of “Those Things About C++”. The content is now synchronized to Station B and displayed in the form of a video. You can open the website to read it directly~ Welcome everyone to star, repost, and PR.

Website: C++ those things

  • Chinese name: C++ those things
  • English name: Stories About C Plus Plus

This is a warehouse suitable for beginners from entry to advanced, solving the problem of interviewers and learners who want to go deeper into C++ and how The problem of getting into C++ . In addition, this warehouse expands more in-depth source code analysis, multi-threaded concurrency, etc., and is a relatively comprehensive warehouse for learning C++ from entry to advanced.

Cmake official tutorial (translation)-mianshigee.com (mianshigee.com)

A cheat sheet for modern C++ language and library functions – mianshigee.com (mianshigee.com)

GitHub – AnthonyCalandra/modern-cpp-features: A cheatsheet of modern C++ language and library features.

Programming Methods: Interviews and Algorithm Experiences-mianshigee.com (mianshigee.com)

GitHub – julycoding/The-Art-Of-Programming-By-July-2nd: This project once ranked first in the world. A collection of useful information can be found at the bottom of this page. There is also a complete and exquisite paper version of “The Method of Programming: Interviews and Algorithms” “Experiences” has been sold on JD.com/Dangdang

GitHub – chunhuajiang/arm-gcc-inline-assembler: ARM GCC Inline Assembly Reference Manual – Chinese version

C++ thread methods and properties:

  1. joinable() determines whether a thread can be connected (executable thread). If the following conditions occur, it is unconnectable:

    1. When constructed, thread() has no parameters;
    2. The object’s thread has been moved;
    3. The thread has been join or detach;
  2. get_id() returns the ID of the thread;

  3. native_handle() returns the POSIX standard thread object;

  4. join() Waits for thread execution to complete;

  5. detach() Detaches the thread. After detachment, the object no longer owns the thread. After the thread ends, the memory will be automatically recycled. (It will not start another process);

  6. swap() Threads that exchange objects.

C++20 jthread:

Based on std::thread, std::jthread adds a new feature that can actively cancel or stop thread execution. std::jthread has an exception-safe thread termination process compared to std::thread and can be replaced in most cases with little or no code changes.

A std::thread instance can be in either the joinable or unjoinable state. A std::thread that is default constructed, detached, or moved is unjoinable. We must join a joinable std::thread explicitly before the end of its life; otherwise, the std::thread’s destructor calls std::terminate, whose default behavior is to abort the process. A std::thread instance can be in a joinable or unjoinable state. A default-constructed, detached, or moved std::thread is not joinable. We must explicitly join the connectable std::thread before its lifetime ends; otherwise, the std::thread’s destructor will call std::terminate, whose default behavior is to abort the process.

void FuncWithoutJoinOrDetach() {

  std::thread t{task, task_args};

  // No t.join() or t.detach() called

} // std::terminate() will be called when the life cycle of t ends, and the program will end abnormally

In C++11, if the thread object is in a joinable state before being destroyed, but there is no join, an exception will be thrown.

As shown in the above code, if t.join() or t.detach() is not called, when the life cycle of the thread object t ends, a core dump may be generated, causing the program to terminate abnormally. In the above example, after instantiating object t, even if the join() function of thread t is called, sometimes it may take a long time to complete the task execution of thread t, or even wait forever (for example, there is an infinite loop in the task ), since thread does not allow us to actively kill it like a process, so when an infinite loop occurs in t, it will result in the inability to continue executing the statements after jion(). The started thread can only end its own operation or end the entire program. End this thread. Based on the above two main reasons, the std::jthread class was introduced in C++20 to make up for the shortcomings of std::tread. In addition to having the behavior of std::thread, it mainly adds the following two new functions:

  • When the std::jthread object is destructed, join will be automatically called to wait for the execution flow it represents to end.
  • std::jthread supports external request abort (via get_stop_source, get_stop_token and request_stop).

The automatic join and external request abort features in std::jthread make it easier to write safer code, but it also adds overhead to performance relative to thread.

The obvious difference between thread and jthread is that std::jthread will determine whether the thread is still running joinable during deconstruction. If it is still running, request_stop and join are automatically called.

Simple implementation of jthread’s destructor:

In addition, std::jthread also provides a built-in std::stop_token. It can be obtained through the first parameter of the thread function (if the first parameter type of the function is std::stop_token).

It can be operated through methods such as get_stop_source, get_stop_token, and request_stop .

stop_token is similar to a signal that tells the thread whether it is time to end. Used with stop_source . stop_token is used to obtain whether to exit, and stop_source is used to request exit. Its method:

Summarize:

jthread is a thread encapsulated by the RAII mechanism, which will automatically call join during destruction to prevent thread crashes.

Do you know all these new features of C++20? – Zhihu (zhihu.com)

Thread – Essay Category – ink19 – Blog Park (cnblogs.com)