Online Book Reader

Home Category

Professional C__ - Marc Gregoire [402]

By Root 1290 0
you cannot use atomic types and operations, you have to use a locking mechanism to make sure reads and writes between different threads are synchronized.

Release locks as soon as possible: When you need to protect your shared data with a lock, make sure you release the lock as soon as possible. While a thread is holding a lock, it is blocking other threads waiting for the same lock, possibly hurting performance.

Make sure to acquire multiple locks in the same order: If multiple threads need to acquire multiple locks, they must be acquired in the same order in all threads to prevent deadlocks. You can use the generic std::lock() specifying the locks in the same order to minimize the chance that you will violate lock ordering restrictions.

Use a multithreading aware profiler: Use a multithreading aware profiler to find performance bottlenecks in your multithreaded applications and to find out if your multiple threads are indeed utilizing all available CPU cores in your system. An example of a multithreading aware profiler is the profiler in Visual Studio 2010 Premium or Ultimate.

Understand the multithreading support features of your debugger: Most debuggers have at least basic support for debugging multithreaded applications. You should be able to get a list of all running threads in your application and you should be able to switch to any of those threads to inspect their call stack. You can use this, for example, to inspect deadlocks because you can see exactly what each thread is doing.

Use thread pools instead of creating and destroying a lot of threads dynamically: Your performance decreases if you dynamically create and destroy a lot of threads. In that case it’s better to use a thread pool to reuse existing threads.

SUMMARY


This chapter gave a brief overview of multithreaded programming using the C++11 threading library. It explained how you can use atomic types and atomic operations to operate on shared data without you having to use explicit locks. In case you cannot use these atomic types and operations, you learned how to use a mutual exclusion mechanism to ensure proper synchronization between different threads that need read/write access to shared data. You also saw how promises and futures represent a simple inter-thread communication channel; you can use futures to more easily get a result from a background thread. The chapter finished with a number of best practices for multithreaded application design.

As mentioned in the introduction, this chapter tried to touch on all the functionality provided by the C++11 threading library, but due to space constraints, it cannot go into all the details of multithreaded programming. There are books available that discuss nothing but multithreading. See Appendix B for a few references.

PART IV

C++ Software Engineering

CHAPTER 23: Maximizing Software Engineering Methods

CHAPTER 24: Writing Efficient C++

CHAPTER 25: Developing Cross-Platform and Cross-Language Applications

CHAPTER 26: Becoming Adept at Testing

CHAPTER 27: Conquering Debugging

CHAPTER 28: Incorporating Design Techniques and Frameworks

CHAPTER 29: Applying Design Patterns

Chapter 23

Maximizing Software Engineering Methods


WHAT’S IN THIS CHAPTER?

What a software life cycle model is, with examples of the Stagewise Model, the Waterfall Model, the Spiral Model, and RUP

What software engineering methodologies are, with examples of Agile, Scrum, XP, and Software Triage

What Source Code Control means

Chapter 23 starts the last part of this book, which is about software engineering. This part describes software engineering methods, code efficiency, cross-platform development, software testing, software debugging, design techniques, and design patterns.

When you first learned how to program, you were probably on your own schedule. You were free to do everything at the last minute if you wanted to, and you could radically change your design during implementation. When coding in the professional world, however, programmers rarely have such flexibility. Even the most liberal

Return Main Page Previous Page Next Page

®Online Book Reader