Книга: Programming with POSIX® Threads
1.7.2 Programming discipline
1.7.2 Programming discipline
Despite the basic simplicity of the threaded programming model, writing real-world code is never trivial. Writing code that works well in multiple threads takes careful thought and planning. You have to keep track of synchronization protocols and program invariants. You have to avoid deadlocks, races, and priority inversions. I'll describe all of these things in later sections, show how to design code to avoid the problems, and how to find and repair them after the fact.
You will almost certainly use Library code that you did not write. Some will be supplied with the operating system you use, and most of the more common libraries will likely be safe to use within multiple threads. POSIX guarantees that most functions specified by ANSI C and POSIX must be safe for use by multithreaded applications. However, a lot of "interesting" functions you will probably need are not included in that list. You will often need to call libraries that are not supplied with the operating system, for example, database software. Some of that code will not be thread-safe. I will discuss techniques to allow you to use most unsafe code, but they will not always work, and they can be ugly.
All threads within a process share the same address space, and there's no protection boundary between the threads. If a thread writes to memory through an uninitialized pointer, it can wipe out another thread's stack, or heap memory being used by some other thread. The eventual failure will most likely occur in the innocent victim, possibly long after the perpetrator has gone on to other things. This can be especially important if arbitrary code is run within a thread. For example, in a library that supports callbacks to functions supplied by its caller, be sure that the callback, as well as the library, is thread-safe.
The important points are that good sequential code is not necessarily good threaded code, and bad threaded code will break in ways that are more difficult to locate and repair. Thinking about real-life parallelism can help a lot, but programming requires a lot more detailed work than most things in real life.
- PART V Programming Linux
- Programming in C
- Using the C Programming Project Management Tools Provided with Fedora Linux
- 14.4.1. Programming Flash Using a JTAG Probe
- 3.2 PIC Microcontroller Input-Output Port Programming
- 3.3 Programming Examples
- 9.11 CAN Bus Programming
- Table B-1. Support for Java, C, and C++ programming
- Задание Begin3 из электронного задачника Programming Taskbook
- Chapter 11. Emacs Lisp Programming
- 9. Programming – using machine code and assembly
- Peter Naur, Programming as Theory Building