From 232aba20806c5ae2b06fbfd5dadbf82871e4a30e Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Mon, 12 Feb 2018 23:52:03 +1300 Subject: [PATCH 1/2] Wait for spawned thread A spawned thread doesn't block the main thread exiting unless explicitly joined. The demo as written in the guide simply exits immediately at the moment. --- guide/src/qs_2.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/guide/src/qs_2.md b/guide/src/qs_2.md index 80852895e..10a50e393 100644 --- a/guide/src/qs_2.md +++ b/guide/src/qs_2.md @@ -79,13 +79,14 @@ fn index(req: HttpRequest) -> &'static str { } fn main() { -# thread::spawn(|| { +# let child = thread::spawn(|| { HttpServer::new( || Application::new() .resource("/", |r| r.f(index))) .bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088") .run(); -# }); + }); +# child.join().expect("failed to join server thread"); } ``` From b28ecbcf0c6ed07cb041f61500870aa6c3877a03 Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Wed, 14 Feb 2018 10:37:12 +1300 Subject: [PATCH 2/2] Update qs_2.md --- guide/src/qs_2.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/guide/src/qs_2.md b/guide/src/qs_2.md index 10a50e393..ac53d8707 100644 --- a/guide/src/qs_2.md +++ b/guide/src/qs_2.md @@ -71,7 +71,7 @@ Here is full source of main.rs file: ```rust # use std::thread; -# extern crate actix_web; +extern crate actix_web; use actix_web::*; fn index(req: HttpRequest) -> &'static str { @@ -79,14 +79,16 @@ fn index(req: HttpRequest) -> &'static str { } fn main() { -# let child = thread::spawn(|| { +# // In the doctest suite we can't run blocking code - deliberately leak a thread +# // If copying this example in show-all mode make sure you skip the thread spawn +# // call. +# thread::spawn(|| { HttpServer::new( || Application::new() .resource("/", |r| r.f(index))) .bind("127.0.0.1:8088").expect("Can not bind to 127.0.0.1:8088") .run(); - }); -# child.join().expect("failed to join server thread"); +# }); } ```