Online Book Reader

Home Category

Professional C__ - Marc Gregoire [200]

By Root 1318 0
processes);

// Selects the next process using a round-robin scheduling

// algorithm and allows it to perform some work during

// this time slice.

void scheduleTimeSlice();

// Removes the given process from the list of processes.

void removeProcess(const Process& process);

protected:

RoundRobin rr;

};

Scheduler::Scheduler(const vector& processes)

{

// Add the processes

for (auto& process : processes) {

rr.add(process);

}

}

void Scheduler::scheduleTimeSlice()

{

try {

rr.getNext().doWorkDuringTimeSlice();

} catch (const out_of_range&) {

cerr << "No more processes to schedule." << endl;

}

}

void Scheduler::removeProcess(const Process& process)

{

rr.remove(process);

}

int main()

{

vector processes = {Process("1"), Process("2"), Process("3")};

Scheduler sched(processes);

for (int i = 0; i < 4; ++i)

sched.scheduleTimeSlice();

sched.removeProcess(processes[1]);

cout << "Removed second process" << endl;

for (int i = 0; i < 4; ++i)

sched.scheduleTimeSlice();

return 0;

}

Code snippet from RoundRobin\RoundRobinTest.cpp

The output should be as follows.

Process 1 performing work during time slice.

Process 2 performing work during time slice.

Process 3 performing work during time slice.

Process 1 performing work during time slice.

Removed second process

Process 3 performing work during time slice.

Process 1 performing work during time slice.

Process 3 performing work during time slice.

Process 1 performing work during time slice.

The vector Specialization

The standard requires a partial specialization of vector for bools, with the intention that it optimizes space allocation by “packing” the Boolean values. Recall that a bool is either true or false, and thus could be represented by a single bit, which can take on exactly two values. C++ does not have a native type that stores exactly one bit. Some compilers represent a Boolean value with a type the same size as a char. Some other compilers use an int. The vector specialization is supposed to store the “array of bools” in single bits, thus saving space.

You can think of the vector as a bit-field instead of a vector. The bitset container described later in this chapter provides a more full-featured bit-field implementation than does vector. However, the benefit of vector is that it can change size dynamically.

In a half-hearted attempt to provide some bit-field routines for the vector, there is one additional method: flip(). This method can be called on either the container, in which case it complements all the elements in the container; or on a single reference returned from operator[] or a similar method, in which case it complements that single element.

At this point, you should be wondering how you can call a method on a reference to bool. The answer is that you can’t. The vector specialization actually defines a class called reference that serves as a proxy for the underlying bool (or bit). When you call operator[], at(), or a similar method, the vector returns a reference object, which is a proxy for the real bool.

The fact that references returned from vector are really proxies means that you can’t take their addresses to obtain pointers to the actual elements in the container. The proxy design pattern is covered in detail in Chapter 29.

In practice, the little amount of space saved by packing bools hardly seems worth the extra effort. However, you should be familiar with this partial instantiation because of the additional flip() method, and because of the fact that references are actually proxy objects. Many C++ experts recommend avoiding vector in favor of the bitset, unless you really need a dynamically sized bit-field.

deque

The deque (abbreviation for double-ended queue) is almost identical to the vector, but is used far less frequently. It is defined in the header file. The principle differences are:

The implementation is not required to store elements contiguously in memory.

The deque supports constant-time

Return Main Page Previous Page Next Page

®Online Book Reader