Listing 8: The Printer Class #include #include #include "printer.hpp" // Constructor printer::printer() { // Arrange to catch SIGUSR1 using the calcFinished method struct sigaction act; act.sa_handler = printer::calcFinished; sigfillset(&(act.sa_mask)); sigaction(SIGUSR1, &act, NULL); } // Signal handler for catching Calculator completed signal (SIGUSR1) void printer::calcFinished(int sigNo) { // If the signal is SIGUSR1 just return and let the thread continue if (SIGUSR1 == sigNo) return; // Otherwise terminate the thread else thr_exit(NULL); } // Retrieve powers of 2 from the calculator queue and print them void printer::printPowersOfTwo(calculator *calc) { // Loop continually, until the thread is killed while(true) { // Wait for the data ready signal from the calculator mutex_lock(&calc->queueReady); cond_wait(&calc->signalQueueReady, &calc->queueReady); // Retrieve and display values from the queue while (!calc->power2Queue.empty()) { int data = calc->power2Queue.front(); calc->power2Queue.pop(); cout << printerName << ":" << data << endl; } // Indicate that the queue is now empty mutex_unlock(&calc->queueReady); } }