Summary

In this chapter, we finally started learning to write parallel code. I started with a short introduction about processes and threads, single and multithreading, single-tasking, and multitasking. I also explained the most important differences between processes and threads.

After that, we started learning what not to do when writing multithreaded code. Firstly, I brought up the most important dogma—"Never access the user interface from a background thread". Such strong words deserve proof and I gave you one.

In the next, largest part of the chapter, I slowly explained why you should be extremely careful if you want to access shared data from multiple threads. While simultaneous reading is OK, you should always use protection when reading and writing at the same time.

In the context of parallel programming and data sharing, this protection is implemented by the introduction of one of the synchronization mechanisms. I spent quite some time introducing critical sections, mutexes, semaphors, TMonitor, spinlocks, and the terribly named TMultiReadExclusiveWriteSynchronizer. Using synchronization inevitably brings in new problems, and so I also spent some time discussing deadlocks. I also mentioned three data structures that implement locking internally—TThreadList, TThreadList<T>, and TThreadedQueue<T>.

After that I introduced a less powerful but faster way of locking—interlocked operations. We examined the TInterlocked class and all the interlocked operations it implements. As interlocked operations are frequently used in optimistic initialization, we also dealt with the object life cycle in the parallel world.

Towards the end, I introduced a much faster replacement for synchronization—communication. We saw how a parallel program can use data duplication and aggregation to achieve the highest possible speed. I went over four communication techniques, namely Windows messages, Synchronize, Queue, and polling. We also saw how TThreadedQueue<T> can be used in practice.

To wrap things up, I introduced two libraries that contain helpful parallel programming helpers—Spring4D and OmniThreadLibrary.

In the next chapter, we'll finally start writing parallel code. I'll talk about threads, tasks, and writing parallel code with TThread and with the Parallel Programming Library, which has been part of Delphi since release XE7.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.114.154