The following C++ code demonstrates how we can implement the threading program. It shows that there are some subtle differences between C and C++ with regard to outputting strings to the screen. However, as you will discover, C functionality can also be implemented in C++.
It also importantly introduces us to thread termination via the pthread_exit
function.
As you will encounter C and C++ used heavily in many hardware projects designed for the Raspberry Pi, this program will give you a basic introduction to C++, which you can use as a jumping off point.
You can read more about C++ at http://www.cplusplus.com/doc/ and find a wealth of tutorials.
Our next task is going to be to create a new file under your c_programs
directory:
vim first_cpp_prog.cpp
Let's start by adding the include
statements to the top of the file:
#include <iostream> #include <cstdlib> #include <pthread.h>
Here we include a library to handle input and output, called iostream
. This provides...