complete threads till 06
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
fn intro() -> &'static str {
|
fn intro() -> &'static str {
|
||||||
// TODO: fix me 👇
|
// TODO: fix me 👇
|
||||||
"I'm ready to _!"
|
"I'm ready to build a concurrent ticket management system!"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -15,7 +15,14 @@
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
pub fn sum(v: Vec<i32>) -> i32 {
|
pub fn sum(v: Vec<i32>) -> i32 {
|
||||||
todo!()
|
// let mut c1 = (v.clone()[..v.len() / 2]).iter().collect::<Vec<_>>();
|
||||||
|
// let mut c2 = (v.clone()[v.len() / 2..]).iter().collect::<Vec<_>>();
|
||||||
|
let (c1, c2) = v.split_at(v.len() / 2);
|
||||||
|
let c1 = c1.to_vec();
|
||||||
|
let c2 = c2.to_vec();
|
||||||
|
let t1 = std::thread::spawn(move || c1.iter().sum::<i32>());
|
||||||
|
let t2 = std::thread::spawn(move || c2.iter().sum::<i32>());
|
||||||
|
t1.join().unwrap() + t2.join().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -4,7 +4,10 @@
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
pub fn sum(slice: &'static [i32]) -> i32 {
|
pub fn sum(slice: &'static [i32]) -> i32 {
|
||||||
todo!()
|
let (s1, s2) = slice.split_at(slice.len() / 2);
|
||||||
|
let t1 = std::thread::spawn(|| s1.iter().sum::<i32>());
|
||||||
|
let t2 = std::thread::spawn(|| s2.iter().sum::<i32>());
|
||||||
|
t1.join().unwrap() + t2.join().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -6,7 +6,11 @@
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
pub fn sum(v: Vec<i32>) -> i32 {
|
pub fn sum(v: Vec<i32>) -> i32 {
|
||||||
todo!()
|
let lv = v.leak();
|
||||||
|
let (s1, s2) = lv.split_at(lv.len() / 2);
|
||||||
|
let t1 = std::thread::spawn(|| s1.iter().sum::<i32>());
|
||||||
|
let t2 = std::thread::spawn(|| s2.iter().sum::<i32>());
|
||||||
|
return t1.join().unwrap() + t2.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -3,7 +3,13 @@
|
|||||||
// Don't perform any heap allocation. Don't leak any memory.
|
// Don't perform any heap allocation. Don't leak any memory.
|
||||||
|
|
||||||
pub fn sum(v: Vec<i32>) -> i32 {
|
pub fn sum(v: Vec<i32>) -> i32 {
|
||||||
todo!()
|
let mid = v.len() / 2;
|
||||||
|
let (s1, s2) = v.split_at(mid);
|
||||||
|
std::thread::scope(|scope| {
|
||||||
|
let t1 = scope.spawn(|| s1.iter().sum::<i32>());
|
||||||
|
let t2 = scope.spawn(|| s2.iter().sum::<i32>());
|
||||||
|
t1.join().unwrap() + t2.join().unwrap()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
use std::sync::mpsc::{Receiver, Sender};
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
|
|
||||||
|
use data::TicketDraft;
|
||||||
|
use store::TicketStore;
|
||||||
|
|
||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod store;
|
pub mod store;
|
||||||
|
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
Insert(todo!()),
|
Insert(TicketDraft),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the system by spawning the server thread.
|
// Start the system by spawning the server thread.
|
||||||
@@ -20,4 +23,14 @@ pub fn launch() -> Sender<Command> {
|
|||||||
// Enter a loop: wait for a command to show up in
|
// Enter a loop: wait for a command to show up in
|
||||||
// the channel, then execute it, then start waiting
|
// the channel, then execute it, then start waiting
|
||||||
// for the next command.
|
// for the next command.
|
||||||
pub fn server(receiver: Receiver<Command>) {}
|
pub fn server(receiver: Receiver<Command>) {
|
||||||
|
let mut tickStor = TicketStore::new();
|
||||||
|
'aLoop: loop {
|
||||||
|
let command = receiver.recv().ok().unwrap();
|
||||||
|
match command {
|
||||||
|
Command::Insert(a) => {
|
||||||
|
tickStor.add_ticket(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ fn ready() {
|
|||||||
// since our server doesn't expose any **read** actions.
|
// since our server doesn't expose any **read** actions.
|
||||||
// We have no way to know if the inserts are actually happening and if they
|
// We have no way to know if the inserts are actually happening and if they
|
||||||
// are happening correctly.
|
// are happening correctly.
|
||||||
let move_forward = false;
|
let move_forward = true;
|
||||||
|
|
||||||
assert!(move_forward);
|
assert!(move_forward);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,18 +6,18 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
pub struct DropTracker<T> {
|
pub struct DropTracker<T> {
|
||||||
value: T,
|
value: T,
|
||||||
counter: todo!(),
|
counter: Rc<RefCell<usize>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> DropTracker<T> {
|
impl<T> DropTracker<T> {
|
||||||
pub fn new(value: T, counter: todo!()) -> Self {
|
pub fn new(value: T, counter: Rc<RefCell<usize>>) -> Self {
|
||||||
Self { value, counter }
|
Self { value, counter }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Drop for DropTracker<T> {
|
impl<T> Drop for DropTracker<T> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
todo!()
|
*self.counter.borrow_mut() += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
use data::{Ticket, TicketDraft};
|
||||||
use std::sync::mpsc::{Receiver, Sender};
|
use std::sync::mpsc::{Receiver, Sender};
|
||||||
|
use store::TicketId;
|
||||||
|
|
||||||
use crate::store::TicketStore;
|
use crate::store::TicketStore;
|
||||||
|
|
||||||
pub mod data;
|
pub mod data;
|
||||||
@@ -6,8 +9,14 @@ pub mod store;
|
|||||||
|
|
||||||
// Refer to the tests to understand the expected schema.
|
// Refer to the tests to understand the expected schema.
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
Insert { todo!() },
|
Insert {
|
||||||
Get { todo!() }
|
draft: TicketDraft,
|
||||||
|
response_sender: Sender<TicketId>,
|
||||||
|
},
|
||||||
|
Get {
|
||||||
|
id: TicketId,
|
||||||
|
response_sender: Sender<Option<Ticket>>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn launch() -> Sender<Command> {
|
pub fn launch() -> Sender<Command> {
|
||||||
@@ -21,19 +30,25 @@ pub fn server(receiver: Receiver<Command>) {
|
|||||||
let mut store = TicketStore::new();
|
let mut store = TicketStore::new();
|
||||||
loop {
|
loop {
|
||||||
match receiver.recv() {
|
match receiver.recv() {
|
||||||
Ok(Command::Insert {}) => {
|
Ok(Command::Insert {
|
||||||
todo!()
|
draft,
|
||||||
|
response_sender,
|
||||||
|
}) => {
|
||||||
|
let id = store.add_ticket(draft);
|
||||||
|
response_sender.send(id);
|
||||||
}
|
}
|
||||||
Ok(Command::Get {
|
Ok(Command::Get {
|
||||||
todo!()
|
id,
|
||||||
|
response_sender,
|
||||||
}) => {
|
}) => {
|
||||||
todo!()
|
let tick = store.get(id);
|
||||||
|
response_sender.send(tick.cloned());
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
// There are no more senders, so we can safely break
|
// There are no more senders, so we can safely break
|
||||||
// and shut down the server.
|
// and shut down the server.
|
||||||
break
|
break;
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user