added sources and a quote to notes

This commit is contained in:
Stephen M. McQuay 2012-05-25 10:06:00 -06:00
parent 9a5202821d
commit 4a84a1fe84
3 changed files with 14 additions and 12 deletions

View File

@ -75,6 +75,7 @@ regular expressions and threading
- this is where the burning starts to set in ... - this is where the burning starts to set in ...
- threading works ... mostly - threading works ... mostly
- `great quote`_
- compiles and runs with both compilers - compiles and runs with both compilers
- run stress - run stress
- regex examples - regex examples
@ -83,6 +84,7 @@ regular expressions and threading
- instructions on building this found `here`_ - instructions on building this found `here`_
.. _`here`: http://solarianprogrammer.com/2011/10/16/llvm-clang-libc-linux/ .. _`here`: http://solarianprogrammer.com/2011/10/16/llvm-clang-libc-linux/
.. _`great quote`: https://twitter.com/nedbat/status/194452404794691584
constexpr constexpr
========= =========

View File

@ -1,18 +1,17 @@
// based on: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/
#include <iostream> #include <iostream>
#include <thread> #include <thread>
//This function will be called from a thread
void call_from_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() { int main() {
//Launch a thread
std::thread t1(call_from_thread); std::thread t1(call_from_thread);
std::cout << "printing from original thread" << std::endl;
//Join the thread with the main thread
t1.join(); t1.join();
std::cout << "after join" << std::endl;
return 0; return 0;
} }

View File

@ -1,25 +1,26 @@
// from: http://solarianprogrammer.com/2011/12/16/cpp-11-thread-tutorial/
#include <iostream> #include <iostream>
#include <thread> #include <thread>
static const int num_threads = 10; static const int num_threads = 8;
//This function will be called from a thread
void call_from_thread(int tid) { void call_from_thread(int tid) {
std::cout << "Launched by thread " << tid << std::endl; std::cout << "Launched by thread " << tid << std::endl;
double x = 1.0;
for(int i = 0; i < 2E9; i++) {
x /= 2;
}
} }
int main() { int main() {
std::thread t[num_threads]; std::thread t[num_threads];
//Launch a group of threads
for (int i = 0; i < num_threads; ++i) { for (int i = 0; i < num_threads; ++i) {
t[i] = std::thread(call_from_thread, i); t[i] = std::thread(call_from_thread, i);
} }
std::cout << "Launched from the main\n"; std::cout << "Launched from the main\n";
//Join the threads with the main thread
for (int i = 0; i < num_threads; ++i) { for (int i = 0; i < num_threads; ++i) {
t[i].join(); t[i].join();
} }