Using Threads
This section uses a simple example to introduce and explain the concepts and classes involved in creating threads. You can find more examples in buildspace/examples/package-name. The following list gives the names of those examples and the classes and functions that they exercise:
The scope and completeness of some examples in this guide are limited to ensure readability and satisfy size constraints. For this reason, you should also review the example programs distributed with the Threading package. See the appendix on file locations in the Building Your Applications document (rcbbd) for the exact location of the examples for your build.
Creating Threads
Example 1 creates a second thread of execution (
main() is the first thread) and uses that thread to call a global function that prints the message “Hello World!” to the console.
Example 1 – Creating threads
#include <rw/thread/RWThreadFunction.h>
#include <iostream> // 1
using namespace std;
void hello(void) // 2
{
cout << "Hello World!” << endl;
}
int main()
{
RWThreadFunction myThread = RWThreadFunction::make(hello); // 3
myThread.start(); // 4
myThread.join(); // 5
return 0;
}
The following sections take a closer look at some of the key components of this example.
Introducing the Runnable
In the Threading package, the runnable family of classes includes the basic mechanisms used to create, control, and monitor the threads of execution within your application. The
RWThreadFunction class used in the example is a member of the runnable family;
Figure 2 shows its relationship to the other runnable classes.
All runnable objects have a start() member and a virtual run() member. The run() member defines the work or activity to be performed by the runnable object. The start() member defines the form of dispatching to be used in executing the run() member. The dispatching is performed either synchronously by executing within the calling thread, or asynchronously by creating a new thread. Regardless of the dispatching mechanism, the run() member is always executed as a consequence of calling start().
The
RWThreadFunction class used in the example is one of the asynchronous or
threaded runnable classes. Instances of this class create a new thread of execution when
start() is called. The
RWThreadFunction class uses a
functor to indicate to the new thread what to execute after it starts; in this case, the
hello() function.
Explicitly Constructing Functors and Runnables
In the following code segment, a functor and threaded runnable instance are explicitly constructed:
Example 2 – Constructing a threaded runnable from a functor
// Construct a functor that invokes hello
RWTFunctor<void()> myFunctor = hello; // 1
// Construct a runnable that invokes functor
RWThreadFunction myThread; // 2
myThread = RWThreadFunction::make(myFunctor); // 3
The code creates a threaded runnable that, when started, launches a thread that invokes the functor instance to execute the hello() function.