Day 24: The 64-Core Sleigh Hustle
“Alright, listen up! The sleigh’s armed with the ElfiX 9000—64 blazing-fast cores. But without a working task queue, we’re going nowhere. I need ideas, and I need them fast!”
Blitzen stretched lazily, balancing a candy cane on his nose. “What’s the big deal? Just make a list, throw everything on there, and let the sleigh do its thing.”
Bernard, the Head Elf, sighed audibly. “Blitzen, a list? Really? We need something efficient, predictable, and thread-safe. We’re not juggling reindeer treats here!”
Santa grunted. “Bernard’s right. This isn’t reindeer games. We need ultimate thread safety and dynamic dispatch. Bernard, go on.”
Bernard adjusted his glasses. “We use a VecDeque. Tasks get pushed to the back and popped from the front. It’s simple and linear.”
Blitzen tilted his head. “And how do we stop the sleigh from pulling a Rudolph and wandering off the rails?”
Bernard rolled his eyes. “Thread safety. Wrap the VecDeque in a Mutex, so each core pops tasks one at a time without stepping on each other’s hooves.”
Santa stroked his beard. “And the tasks? We’ve got a mix: deliveries, routing, even…” he checked the list, “…this goat thing.”
Bernard cut him off. “We implement a SleighTask trait. Each task adheres to it, and the queue just holds Box<dyn SleighTask>. The cores process whatever’s next without worrying about specifics.”
Blitzen yawned. “Sounds overly complicated to me.”
“Spoken like someone who’s never touched a Mutex,” Bernard snapped.
Santa chuckled. “Enough yapping! The VecDeque is our solution. Let’s build it. Christmas flies on this queue!”
The workshop buzzed with determination. With the VecDeque at its heart, the sleigh would soon conquer the skies—no reindeer complaints included.
Your Mission
-
The
SantaSleighQueueshould have a fieldrecords, make sure it is thread safe and can be mutated by multiple threads -
The
recordslist should accept either of the two task types:ElfTaskandReindeerTask -
Both task types should implement the
SleighTasktrait -
The
SantaSleighQueueshould have these methods:new() -> Self: Creates a newSantaSleighQueueenqueueadds a task to the back of the queue, returns()get_taskpops the next task from the front of the queue, returnsOption<T>
-
The
ElfTaskshould have these fields:name: Stringurgency: u32
-
The
ElfTaskshould have anew()associated function that creates a newElfTask -
The
ReindeerTaskshould have these fields:name: Stringweight: u32
-
The
ReindeerTaskshould have anew()associated function that creates a newReindeerTask -
Don't worry about the use of the
urgencyandweightfields, they are just there for demonstration purposes -
You can use
unwrap()on the mutex lock result; for this challenge, you don't need to worry about poisoning. -
Make sure all important values are
pubso they can be accessed from outside the module -
Make sure you have a look at the bottom of the code to see how Santa wants to use the
SantaSleighQueueAPI.
Hints
If you’re unsure where to start, take a look at these tips:
Click to reveal hints!
-
Use a
VecDequeto store the tasks, so that you can push and pop tasks from the front and back. -
Wrap the
VecDequein aMutexto make it thread-safe. -
In order to hold either
ElfTaskorReindeerTaskin theVecDeque, you can useBoxto store a trait object on the heap.pub struct SantaSleighQueue { record: Mutex<VecDeque<Box<dyn SleighTask>>>, } -
Before mutating the
VecDeque, you need to lock theMutexto ensure that only one thread can access it at a time.let mut records = self.records.lock().unwrap(); -
Since
Mutexallows interior mutability, you don't need to get a mutable reference to theselfobject, you can just use&self.pub fn enqueue(&self, task: Box<dyn SleighTask>) { self.record.lock().unwrap().push_back(task); }
Run to execute your code and see test results here.