Add ch02 C++ threading equivalents
This commit is contained in:
2
Makefile
2
Makefile
@@ -70,6 +70,8 @@ SOURCES_CXX=\
|
|||||||
ch01/alarm_cpp.cpp \
|
ch01/alarm_cpp.cpp \
|
||||||
ch01/alarm_fork_cpp.cpp \
|
ch01/alarm_fork_cpp.cpp \
|
||||||
ch01/alarm_thread_cpp.cpp \
|
ch01/alarm_thread_cpp.cpp \
|
||||||
|
ch02/hello_cpp.cpp \
|
||||||
|
ch02/lifecycle_cpp.cpp \
|
||||||
ch03/alarm_mutex_cpp.cpp \
|
ch03/alarm_mutex_cpp.cpp \
|
||||||
ch04/pipe_cpp.cpp
|
ch04/pipe_cpp.cpp
|
||||||
|
|
||||||
|
|||||||
16
src/ch02/hello_cpp.cpp
Normal file
16
src/ch02/hello_cpp.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
void
|
||||||
|
hello()
|
||||||
|
{
|
||||||
|
std::cout << "Hello World\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
std::thread t(hello);
|
||||||
|
t.join();
|
||||||
|
}
|
||||||
24
src/ch02/lifecycle_cpp.cpp
Normal file
24
src/ch02/lifecycle_cpp.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user