I was reading about the PhantomData type and came across the point made that it can be used as a mechanism for controlling lifetimes. From here I started to play around with it a bit and came up with something interesting.

The example itself might be a bit awkward, but bare with me :)

The Scenario

In the example below, we have two threads, one reading (_Reader) _from a file every 100 milliseconds and one writing (Writer) to a file with some pauses in-between writes. The Reader and Writer are independent and have no knowledge of each other.

When the Reader is dropped we want to stop our thread associated with the _Reader. _This is done in the Drop implementation.

#code #rust #concurrency

Controlling Threads using PhantomData in Rust
3.00 GEEK