Continuing on from the last post I am going to discuss threads in a little more detail and more specifically show one way to implement them in C++.
There are a variety of ways to do this but I am going to use the code that was added into C++ Standard Library from the <thread> header. Therefore to compile the code you will require a fairly up to date compiler. Personally I am using Visual Studio 2012. The theory behind this is very similar independent of what you actually use for threading(posix etc) but for this example im sticking to the C++ std library. So Ready to start?
Once you have a funky, new up to date compatible compiler you are ready to look into the goodness of multithreading. But where do I start you ask? What does a multithreaded C++ program even look like you say? Well the answer to these questions is simple. A multithreaded C++ programs looks like any ordinary C++ program and has the usual mix of variables, classes, functions and methods etc. The only real difference is that some of these functions may run concurrently, so there is more emphasis on code management ensuring that shared data is safe for concurrent access. But don't worry too much, I will go over that in more detail in the near future. For now lets have a look at some code!
To start things off let's look at a nice and simple C++ program; Hello World!.
#include <iostream>
int main( int argc, char* argv[] )
{
std::cout << "Hello World!" << std::endl;
return 0;
}
A really nice and simple program that everyone has seen that has coded in C++ for any longer than 5 minutes, and if not...well you really shouldn't probably be trying to learn multithreading in the first place.
All this program does is write "Hello World!" to the standard output stream. Now compare it to the following code.
#include <iostream>
#include <thread> // The new header allowing us to multithread in C++!
void HelloWorld( )
{
std::cout << "Hello Concurrent World!" << std::endl;
}
int main( int argc, char* argv[] )
{
std::thread t(HelloWorld);
t.join( );
return 0;
}
So what's different about this code? This is a multithreaded version of the same code. Yup that's right a multithreaded HelloWorld Program. Booyah!
So what are the real differences in the code then? Well the first difference is the additional header file <thread>. This is the declaration to allow us multithreading support in C++. The functions and classes for managing all the threads are declared in this header file. There are other useful headers that I will discuss in future posts for protecting data and the likes however.
The second difference is that the code for writing the message has now been moved to a separate function. The reason behind this is that every thread has to have an initial function, where the new thread begins. For the initial thread in an application this is the main( ) function, but for every other thread, as specified in the constructor of std::thread objects - in this program the thread object named t, has the function HelloWorld( ) as it's initial function.
Instead of writing directly to the standard output or calling the HelloWorld( ) function from main( ), this program instead launches a new thread to do it, and in doing so brings the total number of threads in the program to two. The initial thread that starts at main( ) and the new thread that starts at HelloWorld( ).
The last and final change is the t.join( ) call. After a new thread has been launched the initial thread continues execution. If it didn't wait until the new thread finished then it would simple reach the return call and exit out of the main function, possibly before the new thread has had a chance to finish what it had to do. This is why we use the call to t.join( ). This ensures that the calling thread(in main( ) ) waits for the new thread to finish before continuing on its merry way.
All of these changes have been highlighted in bold print so as to easily pick them out.
This may seem like a lot of work to simple display a hello world program, and your right it is, and it's certainly not worth multiple threads to do it, but at the same time its a simple program that shows off multhreading in its most basic form.
This is simply a taster of what is to come and in the next post I will go into more detail regarding multithreading and concurrency, given examples of programs that can benefit with running concurrently.
Until then, thanks for reading, James .
No comments:
Post a Comment