[Prev] [Contents] [Next]

Writing and compiling threaded programs

See the articles about `Threads Concepts' in the AS/400 Information Center for introductory language and API neutral information about AS/400 threads.

When writing any code that will use threads or run in a threaded job, you should ensure that ALL APIs or system services you use are thread safe.

Use normal processes for compiling the program. Use the CRTCMOD followed by CRTPGM or CRTSRVPGM commands. You can also use the CRTBNDC CL command to create your threaded program.

In addition, you must define the preprocessor definition _MULTI_THREADED in your source.

The _MULTI_THREADED preprocessor definition is required either in the source file prior to including pthread.h or on the compiler invocation. This preprocessor value allows the system to support both the old model toolkit threads and the new kernel thread APIs in a compatible fashion. The _MULTI_THREADED preprocessor value is used to indicate that your application uses the newer kernel threads model. The preprocessor value may also be used by other parts of the system in order to create thread safe macros. (For example, the fputc() macro of the C runtime could use this preprocessor value. You should always define this macro in source files that will run in a kernel threaded job.)

If you do not define _MULTI_THREADED when you include the pthread.h header file, the system will interpret this as a request to compile for CPA toolkit threads (simulation of threads based on the process model). If the CPA toolkit is not present on the system, your compile will fail.

You should insert the following lines into any module that uses the thread data types or definitions.

#define _MULTI_THREADED
#include <pthread.h>

Header files for Pthread functions

Programs using the Pthread functions must include one or more header files that contain information needed by the functions, such as:

The header files are provided in the QSYSINC library which is optionally installable. Make sure QSYSINC is on your system before compiling programs that use these header files.

In release V4R2, these header files are only available on your system if you have also loaded and applied the limited availability PTF or I-Listed PRPQ that provides the header file support. The PTF number is 5769SS1-J664741. The I-Listed PRPQ number is yet to be provided.

Where to Find Header Files 
Name of Header File Name of File in QSYSINC Name of Member
pthread.h H PTHREAD
sched.h H SCHED
You can display a header file in QSYSINC by using one of the following methods:

STRSEU SRCFILE(QSYSINC/H) SRCMBR(PTHREAD) OPTION(5)
DSPPFM FILE(QSYSINC/H) MBR(SCHED)
You can print a header file in QSYSINC by using one of the following methods:

STRSEU SRCFILE(QSYSINC/H) SRCMBR(PTHREAD) OPTION(6)
CPYF FROMFILE(QSYSINC/H) TOFILE(*PRINT) FROMMBR(SCHED)

Running threaded programs

When you run a threaded program, there are a few requirements that you must be aware of. The job that runs a threaded program must be specially initialized by the system to support threads. Currently, you can start a job that is capable of creating multiple kernel threads using the spawn() API. The spawn() API has a new flag in the spawn inheritance structure that allows you to turn on the multi-thread capability for the child job. In future releases as more of the system is made thread safe, this requirement will be removed.

The QUSRTOOL library also provides source code and an example CL command to allow you to create and use a SPAWN CL command in a fashion that is similar to the SMBJOB CL command. See "SPAWN CL command, QUSRTOOL example" on page 52 for more information.



[Prev] [Contents] [Next]
Copyright © 1998, IBM Corporation. All rights reserved.
Comments? Contact
rchthrds@us.ibm.com