Terminating multiple pthreads and tasks

Terminating a pthread is different from terminating the task that the pthread runs on. The IPT should be the last task to terminate; that is, the IPT should wait for all pthreads and supporting tasks to terminate before it terminates. If the IPT and its associated task should terminate before all its subtasks terminate, those subtasks abend asynchronously with a 33E abend. This type of termination does not allow an orderly cleanup of pthread and task-related resources.

When a process contains multiple pthreads, and one of the executing pthreads starts process termination, the following steps should be taken:
  1. The terminating pthread uses the pthread_quiesce (BPX1PTQ) service to inform all other pthreads that are running in the process of its process termination.
  2. The pthread_quiesce service places the issuing pthread in a wait state until all other pthreads are notified and have terminated.
  3. As each pthread's signal interface routine receives the quiesce notification, it uses the pthread_exit_and_get service to terminate the pthread. The signal interface routine should not pass control to the user program, because it might continue processing. The task that invoked pthread_quiesce is waiting for all the pthreads in the process to terminate.
  4. The pthread that is issuing the pthread_quiesce service gains control after all pthreads have terminated. The terminating pthread can then invoke any exit and cleanup functions that are necessary for an orderly termination of the process.
    Note: The tasks that supported quiesced pthreads can still be running after control is returned to the task that issued pthread_quiesce. Only the pthreads have terminated, not the tasks. Terminating the task is a separate and asynchronous part of terminating the process.
  5. The terminating pthread can then issue a terminating service request such as exit, _exit, or exec. If the terminating pthread is the IPT, the mvsprocclp (BPX1MPC) service can be issued instead of the _exit (BPX1EXI) service. This avoids the automatic termination of the task.
  6. The IPT gains control only when all the pthreads that were created with pthreads_create have terminated. The IPT can then call mvsprocclp (BPX1MPC) to clean up the remaining z/OS UNIX environment. Control cannot return to the IPT until all the other tasks that supported the pthreads have exited. If any of the pthread subtasks fail to terminate, mvsprocclp sets a failing return code.
  7. Now that all the tasks have terminated (except for the IPT), control is returned to the caller of the application (if one exists) or back to the system (which terminates the IPT).