Homework 12: Parallel Programming
For some of you, this will be an introduction to the parallel world that many think is the future of programming. For the rest, this may be second nature.
This is a very basic task to whet your appetite. You must write a program that does the following:
- The program will spawn N threads (command-line argument)
- The main thread should generate 10,000 random numbers between 1 and 20, print them out, and pass them to spawned thread #1 as they are generated
- Spawned thread #T should add T to the numbers it is receiving and pass the new total to thread #T + 1
- The final thread (#N) should add N and print out the final total
In other words, you will create a pipeline of N threads. The main thread will feed in numbers to the first thread; it will add 1 to each number and pass them to the second thread; it will add 2 to each number and pass them on, and so on, until the last thread gets the final sums and displays them.
You can use this as an excuse to check out the parallel programming features in your favorite programming language, or you can give it a try in some of the following languages that have parallel programming features:
Bring your solution to our March meeting.
Feel free to discuss the problem or any other topics you are interested in on our mailing list (accessible via our Meetup Group).