From 4a84a1fe849e68a2e19846744f9830daf2fb49db Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Fri, 25 May 2012 10:06:00 -0600 Subject: [PATCH] added sources and a quote to notes --- notes.rst | 2 ++ thread.cc | 13 ++++++------- thread2.cc | 11 ++++++----- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/notes.rst b/notes.rst index 9648bbf..5a37324 100644 --- a/notes.rst +++ b/notes.rst @@ -75,6 +75,7 @@ regular expressions and threading - this is where the burning starts to set in ... - threading works ... mostly + - `great quote`_ - compiles and runs with both compilers - run stress - regex examples @@ -83,6 +84,7 @@ regular expressions and threading - instructions on building this found `here`_ .. _`here`: http://solarianprogrammer.com/2011/10/16/llvm-clang-libc-linux/ +.. _`great quote`: https://twitter.com/nedbat/status/194452404794691584 constexpr ========= diff --git a/thread.cc b/thread.cc index 2362fec..e84f2a7 100644 --- a/thread.cc +++ b/thread.cc @@ -1,18 +1,17 @@ +// based on: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/ #include #include -//This function will be called from a thread - void call_from_thread() { - std::cout << "Hello, World" << std::endl; + std::cout << "sleeping in the thread" << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + std::cout << "finishing in thread" << std::endl; } int main() { - //Launch a thread std::thread t1(call_from_thread); - - //Join the thread with the main thread + std::cout << "printing from original thread" << std::endl; t1.join(); - + std::cout << "after join" << std::endl; return 0; } diff --git a/thread2.cc b/thread2.cc index 14ce632..2041624 100644 --- a/thread2.cc +++ b/thread2.cc @@ -1,25 +1,26 @@ +// from: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/ #include #include -static const int num_threads = 10; - -//This function will be called from a thread +static const int num_threads = 8; void call_from_thread(int tid) { std::cout << "Launched by thread " << tid << std::endl; + double x = 1.0; + for(int i = 0; i < 2E9; i++) { + x /= 2; + } } int main() { std::thread t[num_threads]; - //Launch a group of threads for (int i = 0; i < num_threads; ++i) { t[i] = std::thread(call_from_thread, i); } std::cout << "Launched from the main\n"; - //Join the threads with the main thread for (int i = 0; i < num_threads; ++i) { t[i].join(); }