CSE 5402: Studio 11

Concurrency: Fork-Join Parallelism, Channels, and Shared Mutable State


In today's studio you will explore fork-join paralellism in combination with file and network IO, how data can be passed using channels, and how mutable state can be shared safely among threads.


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.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.

  2. 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., rustforkjoin).

    Change into the src directory within that package and in the main.rs file that it contains modify the main function so that before the line println!("Hello, world!"); it spawns a child thread that also uses println! to print out a different text message.

    Compile and run your program, and as the answer to this exercise please show the output that was produced.

  3. Modify the main function so that it stores the thread handle returned by the spawn call in a variable, and then at the bottom of the main function matches on the result of a call to the join method on that variable. Compile and run your program, and as the answer to this exercise please show the output that was produced.

  4. Modify the main function so that it iterates through the command line arguments that were passed to it, for each of those arguments except for the one that holds the name of the program spawns a child thread that prints out that argument, stores a handle to each thread in a vector, and then at the bottom of the main function matches on the result of a call to the join method on each handle in the vector.

    Compile and run your program with multiple command line arguments, and as the answer to this exercise please show (1) the code in the main function, and (2) the output that was produced.

  5. Modify the main function so that each child thread treats the text that was passed to it as the name of a file, and tries to open it in a buffered reader (please see the Struct BufReader page for more on buffered readers and how to use them). If that is successful, the child thread should use the buffered reader's lines method to iterate through and print out each non-blank line of the file (i.e., any line that has at least one non-whitespace token), or if the file could not be opened, the child thread should print out a message to that effect instead.

    Compile and run your program with text identifying two files that exist, and two files that do not, as in:

    cargo run Cargo.lock ../Cargo.lock Cargo.toml ../Cargo.toml
    As the answer to this exercise please show (1) the code in the main function, and (2) the output that was produced.
  6. Please see the Networking pages at the end of Chapter 18 in the BOT text book, the TcpListener page, and the TcpStream page, for examples with the kind of code you'll need for this exercise.

    Modify the main function so that it declares a variable initialized with the result of a call to TcpListener::bind, spawns and then joins with a thread that uses that variable to accept a TCP connection, reads and prints out the text sent to it on the accepted connection, and then ends.

    Between spawning that thread and joining with it, the main function should establish a TCP connection by callling TcpStream::connect with the same address used in the call to TcpListener::bind, send all of the command line arguments over that connection, and then call the connection's TcpStream::shutdown method to close it.

    Compile and run your program and as the answer to this exercise show (1) the code you wrote for this exercise, and (2) the new output that code produced.

  7. Modify the thread that runs the TcpListener so that it accepts and uses connections repeatedly in a loop. Compile and run your program and confirm that the program does not terminate until you hit Ctrl-C in the terminal window. As the answer to this exercise please explain briefly why the main thread cannot end until the other thread exits its loop.

  8. Above the main function, declare a static flag of type AtomicBool that is initialized to false. At the bottom of the loop you added in the previous exercise, have the thread check the value of that flag and if it is true the thread should break out of its loop. Modify the main function so that immediately after it calls TcpStream::shutdown it sets the flag to true.

    Compile and run your program and check whether the program terminates normally.

    Modify the main function so that immediately after it sets the flag to true it then connects to the TcpListener one more time (but does not do anything with that connection before joining with the threads).

    Compile and run your program and check whether the program terminates normally.

    As the answer to this exercise please say on which platform you ran this (e.g., on the Linux Lab machines vs. on your laptop) and explain briefly whether making an extra call to connect after setting the flag either (1) had no effect, (2) prevented the program from terminating, or (3) was necessary for the program to terminate correctly.

  9. In the directory for this studio, use the cargo new command to create a new package (named e.g., rustsendsync).

    Change into the src directory within that package and in the main.rs file that it contains modify the main function so that it (1) declares a String variable that contains "hello from the child thread!", (2) moves that String into a child thread that it spawns, (3) stores the thread handle that was returned by spawn in a variable, and then (4) joins with the child thread. The child thread should use println! to print out the String that was moved into it.

    Compile and run your program, and as the answer to this exercise please show the output that was produced.

  10. Modify the main function so that it (1) uses new() to allocate an Rc dynamically on the heap and initializes the Rc with a clone of the String, and then (2) moves the Rc into the child thread. The child thread should print out the String that is referenced by the Rc that was moved into it.

    Try to compile your program, and as the answer to this exercise please (1) show the error that occurred and (2) explain briefly why that happened.

  11. Modify your code so that it uses an Arc instead of an Rc.

    Compile and run your program, and as the answer to this exercise please (1) show the output that was produced and (2) explain briefly why it worked when using an Arc instead of an Rc.

  12. In the top level directory for this studio, use the cargo new command to create a new package (named e.g., rustchannels).

    Modify the main function in the main.rs file in that package so that it declares a mpsc::channel::<String> and uses the let syntax to destructure a tuple into sender and receiver variables. The main function should spawn a child thread, should not send anything into the channel (yet), and should join with the child thread. The child thread should repeatedly call the channel receiver's recv method: if it got Ok from that call it should print out the data it received from the channel and iterate again; otherwise it should break out of the iteration and end the thread.

    Compile and run your program and as the answer to this exercise please (1) describe briefly what happened (if the program does not end you may need to terminate it from the terminal window using Ctrl-C), and (2) explain briefly why you think that happened.

  13. Modify the main function so that it declares several strings and uses the channel to send each of them to the child thread.

    Compile and run your program and as the answer to this exercise please (1) show the output that was produced, and (2) explain why Ctrl-C still may be needed to end the program.

  14. Modify the main function so that after it sends the strings but before it joins with the child thread, it drops the sender.

    Compile and run your program and as the answer to this exercise please (1) show the output that was produced, and (2) explain why the program now terminates on its own without needing Ctrl-C to end it.

  15. Modify your code so that the main function spawns a few more threads and into each of them moves a clone of the original sender to send several different strings into the same channel into which the main thread is sending its unique set of strings. The main thread and each of those other sender threads should call thread::yield_now(); after it sends each string (to yield the OS scheduling slice to another thread), and when it has sent all its strings should drop the sender.

    Compile and run your program and as the answer to this exercise please (1) show the output that was produced, and (2) based on that output please explain at what granularity (i.e., bytes vs. space delimited tokens vs. entire strings vs. all the text from a single sender) the receiver obtained data from the channel.

  16. Modify the main function so that it wraps the receiver in a dynamically allocated Mutex, wraps the Mutex in a dynamically allocated Arc, and moves that Arc into the child thread that reads from the channel. That child thread should repeatedly: acquire the Mutex that is referenced by the Arc, receive from the channel, print out the text it got from the channel, and release the Mutex; if receiving from the channel was not successful, the child thread should break out of its iteration and end.

    Compile and run your program, and as the answer to this exercise please show the output that was produced.

  17. Modify your code so that instead of having only a single receiver thread for the channel, the main function repeats the steps in the previous exercise for a few more reader threads, and joins with all of them too. Compile and run your program, and as the answer to this exercise please show the output that was produced.

  18. Modify your code so that each of the sender threads puts either "[A]" or "[B]" at the start of each chunk of text before it sends it into the channel. Modify the receiver threads so that when they receive a message some of them only print it out if it starts with "[A]" and some only print it out if it starts with "[B]". Compile and run your program, and as the answer to this exercise please show the output that was produced.

  19. Modify your code so that instead of just ignoring an item that does not have the prefix it is looking for, each receiver thread prints it out with the prefix "[delegated]" and then instead of releasing the Mutex it receives another message from the channel and checks it; only when it gets an item with the appropriate prefix should it release the Mutex and begin its next iteration. Compile and run your program, and as the answer to this exercise please show the output that was produced.

Things to Turn In:

For this studio, please turn in the following:


Page updated Tuesday September 22, 2026, by Chris Gill.