|
Thinking In C++. Volume 2: Practical ProgrammingI’m going to stop you and give time to some other thread." This automatic stopping and starting of threads is called preemptive, and it means you (the programmer) don’t need to manage the threading process at all. An alternative approach has each thread voluntarily yield the CPU to the scheduler, which then finds another thread that needs running. Instead, we’ll build the "time-slicing" into the classes in the system. In this case, it will be the tellers that represent the "threads," (the customers will be passive). Each teller will have an infinite-looping run(В ) member function that will execute for a certain number of "time units" and then simply return. By using the ordinary return mechanism, we eliminate the need for any swapping. The resulting program, although small, provides a remarkably reasonable simulation: //: C07:BankTeller.cpp // Using a queue and simulated multithreading // To model a bank teller system #include <cstdlib> #include <ctime> #include <iostream> #include <iterator> ...» | Код для вставки книги в блог HTML
phpBB
текст
|
|