From 9f36a68fba2d3c1e3e7052cc0a4e75f9c569adaa Mon Sep 17 00:00:00 2001 From: David Li Date: Wed, 27 Jan 2016 21:56:53 -0500 Subject: feat: add multithreading --- src/main.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index a230a7e..03f9e02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,8 +5,10 @@ extern crate irc; extern crate serde; extern crate serde_json; +use std::sync::Arc; use std::collections::HashMap; use std::io::Read; +use std::thread; use hyper::Client; use hyper::header::Connection; @@ -74,11 +76,11 @@ fn extract_username(prefix: Option) -> Option { } struct IrcBot { - server: IrcServer, + server: Arc>, } impl IrcBot { - fn new(server: IrcServer) -> IrcBot { + fn new(server: Arc>) -> IrcBot { IrcBot { server: server, } @@ -149,9 +151,19 @@ fn main() { .. Default::default() }; - let server = IrcServer::from_config(config).unwrap(); + let server = Arc::new(IrcServer::from_config(config).unwrap()); server.identify().unwrap(); - let bot = IrcBot::new(server); - bot.process_messages(); + let mut handles = Vec::new(); + for _ in 0..4 { + let s = server.clone(); + handles.push(thread::spawn(move || { + let bot = IrcBot::new(s.clone()); + bot.process_messages(); + })); + } + + for handle in handles.into_iter() { + handle.join(); + } } -- cgit v1.2.3