In today's studio you will explore concurrency design using the Active Object, Half-Sync/Half-Async, and Leader/Followers patterns, in a simple illustrative restaurant example.
Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.
As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then on the Canvas page for this studio assignment upload (1) a file containing your answers and (2) any code you produced while answering the exercises. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
As the answer to the first exercise, list the names of the people who worked together on this studio.
Log in using ssh into shell.cec.wustl.edu using your WUSTL
Key id and password, issue the qlogin command to
get onto one of the Linux Lab machines, and then within the directory you
created for this course, add a new directory for this studio.
In that new directory, use the cargo new command to create a new
package (named e.g., concurrencypatterns).
Change into the src directory within that package and in
the main.rs file that it contains modify the main function
so that instead of printing the line "Hello, world!" it spawns a child
thread that also uses println! to print out "Welcome to AO's place!".
The main function should store the thread handle returned
by the spawn call in a variable, match on the result of a call to the join
method on that variable, and then use println! to print out "Closing time!".
Compile and run your program, and as the answer to this exercise please show the output that was produced.
Outside your main function, declare and define the following struct types with at least the following fields (feel free to add to these at your discretion):
table struct with an unsigned integer table number. That struct should have a
constructor that takes an argument for the table number and assigns it to that field.
waiter struct with a thread handle and a vector of table, a
default constructor, an assign method that takes an argument for a table object and moves it
into the waiter's vector of tables, an activate method that spawns a new thread and stores it
in the waiter's thread handle, and a finish method that joins with the waiter's thread handle.
The newly spawned thread should simply use println! to print out
"Welcome to AO's place!".
Modify the main function so that instead of spawning a thread it (1) creates four waiter objects,
(2) creates 20 table objects each with a unique table number, (3) assigns each waiter five unique
tables, (4) puts each waiter in an Arc, (5) activates each waiter, and (6) calls each waiter's
finish method before again using println! to print out "Closing time!".
Compile and run your code, and as the answer to this question please show the output that was produced.
Outside your main function, declare and define the following struct types with at least the following fields (feel free to add to these at your discretion):
order_item struct with four fields: (1) an unsigned integer for the table
number; (2) an unsigned integer for the seat number; (3) an enumeration for the kind of food
being ordered, with labels chicken, fish, noodles, spinach, carrots, asparagus, yams, salad, and
coleslaw; and (4) an enumeration for the status of the item with labels ordered, ready, and
served. That struct should have a constructor that takes arguments for the table number and seat
number and the third field's enumeration type and assigns them to those fields respectively, and assigns
the fourth field the label ordered.
customer struct with three fields: (1) an unsigned integer table number;
(2) an unsigned integer seat number; and (3) a vector of order_item.
That struct should have a constructor that takes arguments for the table number and the seat number and
assigns them to the first two fields respectively.
Add a vector of customer to the table struct, and a seat
method that constructs a new customer object using the appropriate table number and seat number and moves it into
the vector. Modify the main function so that it also seats a randomly chosen number of between 2 and 6 of
customers at each table.
Add a take_order method to the waiter struct, which takes a reference to an
order_item and moves it into the appropriate customer's vector of order_item structs.
Add an order method to the customer struct, which (1) takes a reference to a waiter object,
(2) constructs an order_item with the customer's table number and seat number and a randomly selected
label for the kind of food being ordered, and then (3) passes that order_item into a call to the
waiter object's take_order method.
Modify the waiter struct's thread function so that for each of its tables it goes to each
customer object at that table and invokes that object's order method. Once it has done
that, the waiter should again iterate through all their customers at all their assigned tables, and use
println! to print out all the fields in each order_item.
Compile and run your code, and as the answer to this question please show the output that was produced.
Outside the main function, declare a (also optionally mutex guarded) chef struct with a thread handle,
an activate function that spawns a new thread and stores it in the chef's thread handle,
and a finish method that joins with the chef's thread handle.
The chef object's newly spawned thread should use println! to print out
"Ready for orders!".
Modify the main function so that before it activates the waiters, it creates a new
chef object that all waiters can access and calls the chef
object's activate method, and after it joins with the waiters it calls the chef
object's finish method.
Compile and run your code, and as the answer to this question please show the output that was produced by the chef object.
Outside the main function, declare a work_item enumeration with two labels:
quit which has no additional information, and process which has an order_item attached.
In the chef struct, add an MPSC channel of work_item, and an order method
that takes an order_item, moves that order_item into a new work_item with a
process label and then moves the work_item into the MPSC channel.
Also add a quit method that moves a new work_item with the label quit into
the chef object's MPSC channel.
Modify the chef struct's thread so that it waits for and takes the next work_item from
its MPSC channel: if the work item's label is quit the chef object should stop;
otherwise it should set the status field of the attached order_item to ready and then
print out the order_item.
Modify the waiter struct's activate method to move a reference to the chef into
its thread, and modify the waiter struct's thread so that instead of printing
out all the fields in each order_item, it passes each order_item into a call to the
chef object's order method.
Modify the main function so that just before it calls the chef object's finish
method it calls the
chef object's quit method.
Compile and run your code, and as the answer to this question please show the output that was produced.
Outside the main function, declare a restaurant struct that holds:
vector of references to the waiters;
HashMap from table numbers to references to each table's waiter; and
Mutex-guarded queue of waiters who are in line to be the leader.
In the main function, replace the chef's creation with the restaurant's, and have the chef's and the
waiters' activate methods move a reference to the restaurant into their threads.
When a waiter finishes delivering all its orders to the chef, it should enqueue itself at the end of the leader queue.
Modify the chef so that instead of printing out each ready item it passes it into a call to the
pickup method of the waiter object that is at the head of that queue.
In the waiter struct, add an MPSC channel of work_item, a quit
method that moves a new work_item with the label quit into the waiter object's MPSC
channel, and a pickup method that takes a work_item and moves it into the waiter's MPSC
channel.
Like the chef object, each waiter object's thread should then take the next work_item from its
MPSC channel, until it has received quit and has served as many items as its customers ordered.
Otherwise for a process work item it
should look up the table number of the attached order_item in the map of tables and waiters and if
it is for one of its own tables should (1) move itself to the end of the leader queue, (2) set the status field
of the attached order_item to served and then (3) print out the order_item;
otherwise it should pass the work_item into a call to the pickup method of the waiter
whose table it is.
Modify the chef struct's thread function so that just before it ends it goes through the
restaurant's vector of references to waiters, and calls each waiter's quit method.
Modify the main function so that once every waiter has delivered all its orders (i.e., is in the
leader queue), it calls the chef object's quit method just before it calls the
chef object's finish method, before it calls each waiter's finish method.
Compile and run your code, and as the answer to this question please show the output that was produced.
For this studio, please turn in the following:
Page created Tuesday October 8, 2026, by Chris Gill and updated Tuesday October 13, 2026, by Jordan Sun.