Project 3: Synchronization

This project consists of two synchronization problems.

Problem 1: Caltrain Automation

Caltrain has decided to improve its efficiency by automating not just its trains but also its passengers. From now on, passengers will be robots. Each robot and each train is controlled by a thread. You have been hired to implement synchronization that will guarantee orderly loading of trains. You must define a Station class that provides three methods:

Here is some additional information for this problem:

Problem 2: Party Introductions

You have been hired by Stanford’s Greek Houses to help manage parties at fraternities and sororities. In particular, you must create a C++ class Party with a single method that can be used to introduce incoming guests to each other. When a guest arrives at a party, they will invoke the following method on a Party object:

std::string meet(std::string &my_name, int my_gender, int other_gender);

The my_name argument gives the name for this guest and my_gender indicates the guest’s gender. The other_gender argument indicates that this guest would like to meet someone of that gender. The meet method must not return until a guest has arrived with matching gender and other_gender, and meet must return the name of the matching guest. In addition:

Developing and Testing

To get started on this project, login to the myth cluster and clone the starter repo with this command:

git clone cs111_p3

This will create a new directory cs111_p3. Do your work for the project in this directory. The directory will contain files and with class and method declarations for the two problems: fill in the existing declarations and add any other definitions or methods that you need.

The directory also contains a Makefile; if you type make, it will compile your code for both problems. will be compiled together with a test program, and will be compiled together with a test program You can invoke the command ./run_tests to run a series of basic tests on your solution. In addition to running tests with run_test, you should also test your Caltrain solution by invoking the following command:

./caltrain_test random

This runs a randomized test with a large number of waiting passengers and a variety of trains with different capacities. Check the output manually to make sure there are no errors. Try running this test many times, in order to generate a variety of scenarios.

We do not guarantee that the tests we have provided are exhaustive, so passing all of the tests is not necessarily sufficient to ensure a perfect score (CAs may discover other problems in reading through your code).

Complexity Penalty

Synchronization code is hard to get right, so it’s important for it to be clean, simple, and obvious. Unfortunately, submissions for this project often end up long and complicated; such solutions rarely work, and in real life they would be brittle and hard to maintain. If the CAs judge your code to be unnecessarily long and complicated, they will deduct up to 20% of the total score for this. Our solution for has 49 lines and our solution for party.c has 38 lines, not including blank lines and comments. Note: your goal should be simplicity, not just line count; simple programs are usually shorter than complex ones, but the shortest program isn’t always the simplest.

Other Requirements

Submitting Your Work

To submit your solutions, cd to the directory containing your work, then invoke the command


If you discover problems or improvements after you have submitted, you may resubmit; only the latest submit will be graded.