Introduction
In this homework, you are going to practice multithreading by simulating a Relay Race. A relay race is a track and field event in which athletes run a pre-set distance carrying a baton before passing it onto the next runner. In the homework, first, runners of the teams will wait for a start signal to start the race together. Then the threads will start and when a runner in the team reaches to destination, the next runner will start. After the last runner in the team reaches to destination, the rank of the team will be updated. Finally, when all teams finish the race, the scoreboard will be printed.
In the homework, you have a struct for Runners as follows:
struct Runner { public:
// VARIABLES // DEFINITIONS float speed; // Runners speed int rid; // Runners ID
int tid; // Runners team ID
bool reached; // Whether runner has reached destination
thread* t; // Runners thread handle
// Constructor
Runner(int rid, int tid):speed(random(4, 8)), rid(rid),tid(tid),reached(false){ //TODO: Start the corresponding thread here in the constructor
// Printing information about it print.lock(); cout << *******
Team id: << tid << tRunner id: << rid <<
Speed:
<< speed <<
*******
;
print.unlock();
}
};
There is also another struct which is for Teams:
struct Team { public:
vector<Runner> teamList; bool finished; int rank; int count;
int currentRacer;
Team(int count):count(count), currentRacer(0), finished(false) {}
};
You are also given main function, start function that threads will run and a helper function, random, for assigning random speed values to runners. You dont need to make any change in the main function, however you are free to edit any part of the given code as long as it works as it should be.
The parts you need to implement is commented with TODO notes. The parts you are responsible for:
- Start the corresponding thread in the constructor of Runner
- Update the runners travelled distance in the start function
- Implement the mechanism to start the next thread (next runner in the team) in the start function
- implement a function to print the rankings
Output
The output for the given main function should be as follows (Dont forget the ranking may be different since we are working on threads, dont expect them to be exactly the same):
*******
Team id: 0 Runner id: 0
Speed: 4.005
*******
*******
Team id: 1 Runner id: 0
Speed: 6.25434
*******
*******
Team id: 2 Runner id: 0
Speed: 4.77322
*******
*******
Team id: 3 Runner id: 0
Speed: 7.23496
*******
***************************************************************************** *
Race starting with the signal now
***************************************************************************** *
*******
Team id: 3 Runner id: 1
Speed: 5.65618
*******
*******
Team id: 1 Runner id: 1
Speed: 7.78698
*******
*******
Team id: 2 Runner id: 1
Speed: 6.78121
*******
*******
Team id: 0 Runner id: 1
Speed: 6.70345
*******
*******
Team id: 1 Runner id: 2
Speed: 5.71648
*******
*******
Team id: 3 Runner id: 2
Speed: 5.98675
*******
*******
Team id: 2 Runner id: 2
Speed: 6.71468
*******
*******
Team id: 0 Runner id: 2
Speed: 4.25098
*******
Team 1 has finished the race
Team 3 has finished the race
Team 2 has finished the race
Team 0 has finished the race
– RANKS –
- Team 1
- Team 3
- Team 2
- Team 0
Press any key to continue . . .
Reviews
There are no reviews yet.