Demo 3.1Basic Threading

[ LiB ]

Now that I've shown you the basics of the ThreadLib, I want to show you how to create a program to demonstrate threading. For this demonstration, I'll show you three threads: The main thread will spawn two child threads, and each child thread will print out 10,000 letters . I'll call these the "a" and "b" threads, since the first one prints out "a"s and the second "b"s.

The demo is located on the CD in the directory /demos/Chapter03/Demo03-01/. The source is contained within Demo03-01.cpp. Instructions for compiling this demo can be found in Appendix A, "Setting Up Your Compilers" (found on the CD).

First, the thread library header file is included:

 #include "ThreadLib/ThreadLib.h" 

After that, the PrintThread function is defined. This function takes a single character as its parameter and prints that 10,000 times:

 void PrintThread( void* data ) {     // convert the data passed in into a character.     char c = (char)data;     for( int i = 0; i < 10000; i++ ) {         cout << c;         cout.flush();     } } 

Since this function is meant to be passed into the ThreadLib::Create() function, the parameter must be of type void* . Since C++ allows you to cast anything into a void*, I exploit this and assume that the void* is actually just a char and not a pointer to anything. So on the fourth line of code, the data is cast into a character.

After that is the loop that runs for 10,000 iterations, printing out one character at a time, and flushing the buffer after each character is printed, so that the character is actually printed to the screen immediately. (If you don't manually flush it, cout tends to buffer a bunch of characters before it actually prints them to screen.)

Now, the main thread:

 int main() {     ThreadLib::ThreadID a, b;     a = ThreadLib::Create( PrintThread, (void*)'a' );     b = ThreadLib::Create( PrintThread, (void*)'b' );     ThreadLib::WaitForFinish( b );     ThreadLib::WaitForFinish( a );     char c;     cin >> c;     return 0; } 

Two thread IDs are created: a and b . Then two new threads are created, both pointing to the PrintThread() function, one with the letter "a" as its parameter, and the other with the letter "b" (both casted into void*s ). Once those threads are created, the main thread calls the WaitForFinish() function to wait until both threads finish executing.

Finally, the last three lines of code are input from the user and return 0. Windows NT has a bad habit of closing a console program window when it finishes, so this procedure prevents that from happening.

Figure 3.9 shows the results of the program when I ran it on my Windows XP computer.

Figure 3.9. Your results probably won't be the same as this screenshot from Demo 3.1 , because all operating systems handle threading differently depending on what you have running at the time.

graphic/03fig09.gif


As you can see from the figure, the two printing threads flip-flop back and forth, each printing a run of around 18 characters before the other thread kicks in.

[ LiB ]


MUD Game Programming
MUD Game Programming (Premier Press Game Development)
ISBN: 1592000908
EAN: 2147483647
Year: 2003
Pages: 147
Authors: Ron Penton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net