I’m new to Rust world and started to learn it only few weeks ago. I came here from C++ and despite of all my experience in programming it was not so easy to start.
To get a feeling what Rust is I decided to rewrite my queue algorithm (GitHub). You can also see some description and discussion (check GitHub page for the link).
In a simple words idea is following. We have a queue that consists of 2 subqueues: one for a writer thread and another for reader one. Writer can freely write to its subqueue and reader can read from its one. And they don’t interfere each other. Writer checks each time after adding new value to subqueue if reader have still what to read and if it doesn’t then writer gives reader its subqueue and starts empty one for itself.
There is one case when writer finishes writing but reader still have what to read. In such a situation writer sets a special flag that shows that it stops to write and reader checks it when its subqueue is empty and reuses writers subqueue to get remaining data.
This algorithm works for one writer and one reader thread configuration and requires only 1 atomic for the readers subqueue head for synchronization.
For the multiple writers and readers there is Guard wrapper that protects writers and readers by using mutex locks. In this case we lock only writer’s group or only reader’s one but those groups don’t block each other.
I tested my algorithm with a simple test and with Relacy Race Detector and looks like it works correctly.
Now I’m trying to rewrite it in Rust (GitHub).
I was using LinkedList and mpsc::Queue structures from the standard Rust library as examples to how to write my queue but stuck with understanding how to use AtomicPtr.
Can someone help me with fixing my implementation?
I’m trying to avoid unsafe blocks as much as possible but not sure if I can write code completely without it.
Also I have a feeling that my approach is not the best one so I will appreciate if someone have a better idea.
Thanx in advance.