Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Concurrency with Modern C++

You're reading from   Concurrency with Modern C++ What every professional C++ programmer should know about concurrency.

Arrow left icon
Product type Paperback
Published in Dec 2019
Publisher
ISBN-13 9781839211027
Length 543 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rainer Grimm Rainer Grimm
Author Profile Icon Rainer Grimm
Rainer Grimm
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

1. Reader Testimonials FREE CHAPTER
2. Introduction 3. Concurrency with Modern C++ 4. Memory Model 5. Multithreading 6. Parallel Algorithms of the Standard Template Library 7. The Near Future: C++20 8. Case Studies 9. The Future: C++23 10. Patterns and Best Practices 11. Synchronisation Patterns 12. Concurrent Architecture 13. Best Practices 14. Lock-Based Data Structures 15. Challenges 16. The Time Library 17. CppMem - An Overview 18. Glossary 19. Index

Dealing with Sharing

If you don’t share, no data races can happen. Not sharing means that your thread works on local variables. This can be achieved by copying the value, by using thread-specific storage, or by transferring the result to the future via a protected data channel. The patterns in this section are quite obvious, but for completeness, I will present them with a short explanation.

Copied Value

If a thread gets its arguments by copy and not by reference, there is no need to synchronise accesses to any data. No data races and no lifetime issues are possible.

Data Races with References

The following program starts three threads. One thread gets its argument by copy, the other by reference and the last by constant reference.

Data races with references
 1 // copiedValueDataRace.cpp
 2 
 3 #include <functional>
 4 #include <iostream>
 5 #include <string>
 6 #include <thread>
 7 
 8 using namespace std::chrono_literals;
 9 
10 void byCopy(bool...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image