From 3005349ac0998e3e5730c6c6a10a41894a1f1a67 Mon Sep 17 00:00:00 2001 From: Bassem Girgis Date: Thu, 18 Oct 2018 14:55:48 -0500 Subject: [PATCH] Add ch02 C++ threading equivalents --- Makefile | 2 ++ src/ch02/hello_cpp.cpp | 16 ++++++++++++++++ src/ch02/lifecycle_cpp.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/ch02/hello_cpp.cpp create mode 100644 src/ch02/lifecycle_cpp.cpp diff --git a/Makefile b/Makefile index c605a02..76c896b 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,8 @@ SOURCES_CXX=\ ch01/alarm_cpp.cpp \ ch01/alarm_fork_cpp.cpp \ ch01/alarm_thread_cpp.cpp \ +ch02/hello_cpp.cpp \ +ch02/lifecycle_cpp.cpp \ ch03/alarm_mutex_cpp.cpp \ ch04/pipe_cpp.cpp diff --git a/src/ch02/hello_cpp.cpp b/src/ch02/hello_cpp.cpp new file mode 100644 index 0000000..73f2367 --- /dev/null +++ b/src/ch02/hello_cpp.cpp @@ -0,0 +1,16 @@ + +#include +#include + +void +hello() +{ + std::cout << "Hello World\n"; +} + +int +main(int argc, char* argv[]) +{ + std::thread t(hello); + t.join(); +} diff --git a/src/ch02/lifecycle_cpp.cpp b/src/ch02/lifecycle_cpp.cpp new file mode 100644 index 0000000..80a054b --- /dev/null +++ b/src/ch02/lifecycle_cpp.cpp @@ -0,0 +1,24 @@ + +#include +#include +#include + +void +thread_routine() +{ + std::cout << "Inside thread" << std::this_thread::get_id() << "\n"; +} + +int +main(int argc, char* argv[]) +{ + std::cout << "Inside thread" << std::this_thread::get_id() << "\n"; + + std::thread t(thread_routine); + + std::this_thread::sleep_for(std::chrono::seconds(1)); + + std::cout << "Main thread created thread " << t.get_id() << "\n"; + + t.join(); +}