CS 321 Spring 2013  >  Assignment 2

CS 321 Spring 2013 Assignment 2

Assignment 2 is due at 5 p.m. Thursday, February 14. It is worth 25 points.

Procedures

E-mail answers to the exercises below to ggchappell@alaska.edu, using the subject “SA2”.

• Your answers should consist of one file: delegate.cpp, from Exercise A. and pipeline.cpp, from Exercise B. This file (or a single archive file containing it) should be attached to your e-mail message.
• I may not read your homework e-mail immediately. If you wish to discuss the assignment (or anything else) with me, send me a separate message with a different subject line.

Exercises (25 pts total)

Exercise A — Delegated Computation

Purpose

In this exercise, you will write a C++ program that performs a number of computations, spawning a separate thread for each.

Instructions

Write a complete C++ program as follows.

• Your program’s source code should be in file delegate.cpp, which should include (“#include”) the header sa2a.h (on the course web page).
• The program should compile and execute in the usual way with a C++11 compiler when header sa2a.h is available for inclusion and the compiler is configured for using the C++11 threads package.
• The program should input a sequence of positive integers from the user, stopping when it receives a value less than 1. For each positive integer, it should call function sa2a with the integer as a parameter, obtain the return value, and print a line in the following form.
sa2a(24) = 37


Here, “24” should be replaced by the user-specified value, and “37” should be replaced by the actual return value. See below for a sample run.

• This is to be a multi-threaded program.
• The program should use the C++11 Standard Library threads facility.
• All I/O is to be done by the master thread.
• Each call to function sa2a should be done by a separate slave thread spawned for this purpose. Each slave thread should make exactly one call to sa2a. The master thread should never call this function.
• Threads must be handled properly, with joining or detaching as appropriate.
• You may arrange the communication between threads any way you like, but, again, do it properly. For example, use locking where this is necessary.
• The slave threads should run concurrently. Each output line (see above) should be printed as soon as the return value is available. Thus, the output lines need not be printed in the order the integers were given by the user—and they usually will not be.
• The program may assume that function sa2s is prototyped as “int sa2a(int);”, and that it is thread-safe (able to be called simultaneously by multiple threads). But you should make no other assumptions about sa2a. So if I rewrite the header to use a different function, then your program should still work.

Sample Run

Here is a sample run of delegate.cpp. Actual return values may be different.

Delegated Computation

Enter a positive integer (or 0 to end input): 1
Enter a positive integer (or 0 to end input): 2
Enter a positive integer (or 0 to end input): 24
Enter a positive integer (or 0 to end input): 3
Enter a positive integer (or 0 to end input): 4
Enter a positive integer (or 0 to end input): 0

sa2a(3) = 1002
sa2a(4) = 6
sa2a(1) = -5
sa2a(24) = 37
sa2a(2) = 6


Coding Standards

The following are standards for all programming assignments in this class.

• Code must compile & execute using a standard-conforming compiler.
• When a test program is given, the code must compile & execute with the test program.

The above requirement is absolute; if your code does not compile, then there is no point in turning it in.

In addition, to receive full credit, submitted code should satisfy the following conditions.

• Code should be neat and readable.
• Code should conform to standard conventions (e.g., regarding the use of header and source files, const-correctness, etc.).
• Comments should be included, indicating filename, authorship, and last revision date of each file, as well as the purpose of each file and each module that is larger than a function (e.g., a C++ class).
• All comments in the code should be correct.
• Code should actually perform the required computations; hacks that only work with the exact input given by the test program will not be counted as fully correct.

CS 321 Spring 2013: Assignment 2 / Updated: 11 Feb 2013 / Glenn G. Chappell / ggchappell@alaska.edu