Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
You can think of a single-threaded program as one lonely
entity moving around through your problem space and doing one thing at a time.
Because there s only one entity, you never have to think about the problem of
two entities trying to use the same resource at the same time: problems such as
two people trying to park in the same space, walk through a door at the same
time, or even talk at the same time.
With multithreading things aren t lonely anymore, but you
now have the possibility of two or more threads trying to use the same resource
at once. This can cause two different kinds of problems. The first is that the
necessary resources may not exist. In C++, the programmer has complete control
over the lifetime of objects, and it s easy to create threads that try to use
objects that get destroyed before those threads complete.
The second problem is that two or more threads may collide
when they try to access the same resource at the same time. If you don t
prevent such a collision, you ll have two threads trying to access the same
bank account at the same time, print to the same printer, adjust the same
valve, and so on.
This section introduces the problem of objects that vanish
while tasks are still using them and the problem of tasks colliding over shared
resources. You ll learn about the tools that are used to solve these problems.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |