Home > Articles

  • Print
  • + Share This
This chapter is from the book

4.7 The Anatomy of a Simple Threaded Program

Any simple multithreaded program will consist of a main or creator thread and the functions that the threads will execute. The thread models determine the manner in which the threads are created and managed. They can be created all at once or under certain conditions. In Example 4.1 the delegation model is used to show a simple multithreaded program.

Example 4.1 Using the delegation model in a simple threaded program.

#include <iostream>
#include <pthread.h>

void *task1(void *X) //define task to be executed by ThreadA
{
   //...
   cout << "Thread A complete" << endl;

}

void *task2(void *X) //define task to be executed by ThreadB

{
   //...
   cout << "Thread B complete" << endl;

}

int main(int argc, char *argv[])
{
   pthread_t ThreadA,ThreadB; // declare threads

   pthread_create(&ThreadA,NULL,task1,NULL); // create threads
   pthread_create(&ThreadB,NULL,task2,NULL);
   // additional processing
   pthread_join(ThreadA,NULL); // wait for threads
   pthread_join(ThreadB,NULL);
   return(0);
}

In Example 4.1, the main line of the example defines the set of instructions for the primary thread. The primary thread, in this case, is also the boss thread. The boss thread declares two threads, ThreadA and ThreadB. By using the pthread_create() function, these two threads are associated with the tasks they are to execute. The two tasks, task1 and task2, are defined. They simply send a message to the standard out but could be programmed to do anything. The pthread_create() function causes the threads to immediately execute their assigned tasks. The pthread_join() function works the same way as wait() for processes. The primary thread waits until both threads return. Figure 4-11 shows the layout of Example 4.1. It also shows what happens to the flow of controls as the pthread_create() and pthread_join() functions are called.

04fig11.gifFigure 4-11. The layout, output, and flow of control for Example .

In Figure 4-11, the pthread_create() function causes a fork in the flow of control in the main line or primary thread. Two additional flows of control, one for each thread, are executing concurrently. The pthread_create() function returns immediately after the threads are created. It is an asynchronous function. As each thread executes its set of instructions, the primary thread executes its instructions. The pthread_join() causes the primary thread to wait until each thread terminates and rejoins the main flow of control.

4.7.1 Compiling and Linking Multithreaded Programs

All multithreaded programs using the POSIX thread library must include the header:

<pthread.h>

In order to compile and link multithreaded applications in the UNIX or Linux environments using the g++ or gcc command-line compilers, be sure to link the Pthreads library to your application. Use the -l option that specifies a library.

-lpthread

will cause your application to link to the library that is compliant with the multithreading interface defined by POSIX 1003.1c standard. The Pthread library, libpthread.so, should be located in the directory where the system stores its standard library, usually /usr/lib. If it is not located in a standard location, use the -L option to make the compiler look in a particular directory first before searching the standard locations.

g++ -o blackboard -L /src/local/lib blackboard.cpp -lpthread
   

tells the compiler to look in the /src/local/lib directory for the Pthread library before searching in the standard locations.

The complete programs in this book are accompanied by a program profile. The program profile contains implementation specifics such as headers and libraries required and compile and link instructions. The profile also includes a note section that will contain any special considerations that need to be taken when executing the program.

  • + Share This
  • 🔖 Save To Your Account