## Detailed Description of Thread Specifics

The program consists of the following general steps:

1. Include the header file pthread.h.

2. Define a parameter block structure type for the thread start routine.

3. Declare arrays for thread handles and parameter blocks.

4. Read the input file line by line.

5. Update the parameter block.

6. Create the thread.

7. Wait for the termination of the thread.

8. Release the resources used by the thread.

8. Define the thread start routine.

9. Declare local variables needed for descriptors for the objects read from the input file.

The following paragraphs describe the corresponding step numbers from the program listing in the next topic:

Step 1 includes the header file pthread.h, which is required for thread programming.

Step 2 defines a parameter block structure type for the thread start routine. A thread start routine must have exactly one parameter. However, add_or_remove( ) requires three parameters (session object, input line and operating mode). The structure pb_add_or_remove is defined as the parameter block for these components. Therefore, the single parameter block contains the three parameters required by add_or_remove( ).

Step 3 declares arrays for thread handles and parameter blocks. The routine which creates the thread (main in this case) must maintain the following information for each thread:

· A thread handle of type pthread_t to identify the thread for join and detach calls.

· A thread specific parameter block that cannot be accessed by any other thread. This makes sure that a parameter for one thread is not overwritten by another thread.

Step 4 reads the input file line by line. A thread is created for each line. A maximum MAX_THREAD_NO of threads are created in parallel. The program then waits for the termination of the created threads so that it can release the resources used by these threads, allowing it to create new threads for remaining input lines (if any).

The absolute maximum number of threads working in parallel depends on system limits; for thradd a value of 10 was chosen (see thradd.h), which is well below the maximum on most systems.

Step 5 updates the parameter block. For each thread a different element of the array of parameter blocks is used.

Step 6 creates the thread. The thread is created by using the function pthread_create( ). The function has the following four parameters:

· The thread handle (output) is stored in an element of the array of type pthread_t.

· For the thread characteristics, the default pthread_attr_default is used.

· The start routine for this thread is add_or_remove( ).

· The parameter passed to add_or_remove( ) is a pointer to an element of the array of parameter blocks.

Step 7 waits for the termination of the thread. The pthread_join( ) routine is called with the thread handle as the input parameter. The program waits for the termination of the thread. If the thread has already terminated, then pthread_join( ) returns immediately. The second parameter of pthread_join( ) contains the return value of the start function; here it's a dummy value because add_or_remove( ) returns a void. add_or_remove( ) is designed as a void function because the calling routine does not have to deal with error cases. The add_or_remove( ) routine prints status messages itself to show the processing order of the threads. Normally a status should be returned to the application.

Step 8 releases the resources used by the thread. The thread handle is used as input for the function pthread_detach( ), which releases the resources (for example, memory) used by the thread.

Step 9 defines the thread start routine. As previously mentioned, the thread start routine must have exactly one parameter. In this case, it is a pointer to the parameter block structure defined in Step 2.

Step 10 declares local variables needed for descriptors for the objects read from the input file. These descriptors are variables and are declared as automatic because of the reentrancy requirement. In the previous sample programs, descriptors were generally declared static. For this example, this is only possible for the constant descriptors declared in thradd.h.

Of course this example shows only a small part of the possibilities of multithreaded XDS programming. For example, each thread could make its own bind which would be useful if more than one GDS server was involved.

##