Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 1.37 KB

tcp-server-log.md

File metadata and controls

36 lines (23 loc) · 1.37 KB

Share data between connections

In this exercise we will take our interactive server and add a common log for lengths of messages that each client sends us. We will explore synchronization primitives that Rust offers in its Standard Library.

After completing this exercise you are able to

  • share data between threads using Mutexes

  • use reference-counting to ensure data stays available across multiple threads

  • use scoped threads to avoid runtime reference counting

  • use channels and message passing to share data among threads by communicating

Tasks

Part 1

  1. Add a log to store length of messages: let mut log: Vec<usize> = vec![];
  2. Pass it to a handle_client function and record a length of each incoming line of text:
    log.push(line.len());
  3. Resolve lifetime issues by using a reference-counting pointer.
  4. Resolve mutability issues by using a mutex

Part 2

  1. Use the thread::scope function to get rid of reference counting for log vector

Part 3

  1. Instead of sharing log vector use a mpsc::channel to send length of lines from worker threads.
  2. Create a separate thread that listens for new channel messages and updates the vector accordingly.