CONTENTS Title Page Copyright Page Preface Part I DECthreads Overview and Programming Guidelines 1 Introduction to Multithreaded Programming 1.1 Overview of Threads 1.2 Advantages of Using DECthreads 1.3 Thread Execution 1.4 Software Models for Multithreaded Programming 1.4.1 Boss/Worker Model 1.4.2 Work Crew Model 1.4.3 Pipelining Model 1.4.4 Combinations of Models 1.5 Potential Problems with Multithreaded Programming 2 Thread Concepts and Operations 2.1 Thread Operations 2.1.1 Starting a Thread 2.1.2 Terminating a Thread 2.1.3 Normal Termination 2.1.4 Error Termination 2.1.5 Waiting for a Thread to Terminate 2.1.6 Deleting a Thread 2.2 Handles 2.3 Attributes Objects 2.3.1 Creating an Attributes Object 2.3.2 Deleting an Attributes Object 2.3.3 Thread Attributes 2.3.3.1 Scheduling Policy Attribute 2.3.3.2 Scheduling Priority Attribute 2.3.3.3 Inherit Scheduling Attribute 2.3.3.4 Stacksize Attribute 2.3.3.5 Guardsize Attribute 2.3.4 Mutex Attributes 2.3.4.1 Mutex Type Attribute 2.3.5 Condition Variable Attributes 2.4 Synchronization Objects 2.4.1 Mutexes 2.4.2 Condition Variables 2.4.3 Other Synchronization Methods 2.5 Atomic Queues 2.6 One-Time Initialization Routines 2.7 Thread-Specific Data 2.8 Thread Cancelation 2.9 Thread Scheduling 3 Programming with Threads 3.1 Nonthreaded Libraries 3.1.1 Working with Nonthreaded Code 3.1.2 Changing Nonthreaded Code to be Thread-Reentrant 3.2 Nonreentrant Code 3.2.1 Global Lock 3.2.2 Thread-Specific Data 3.3 Shared Variables 3.3.1 Static Memory 3.3.2 Dynamic Memory 3.3.3 Stack Memory 3.3.4 Lifetime and Scope of Shared Variables 3.4 Stack Management 3.4.1 Stack Overflow 3.4.2 Sizing the Stack 3.5 Priority Inversion 3.6 Using Synchronization Objects 3.6.1 Mutex or Condition Variable? 3.6.2 Race Conditions 3.6.3 Deadlocks 3.6.4 Signaling a Condition Variable 4 Using the DECthreads Exception Package 4.1 Invoking the pthread Exception-Returning Interface 4.2 Overview of Exceptions 4.2.1 Types of Exceptions 4.2.2 Terminating Exception Semantics 4.3 Exception Operations 4.3.1 Declaring and Initializing an Exception Object 4.3.2 Raising an Exception 4.3.3 Defining a Region of Code Over Which Exceptions Are Caught 4.3.4 Catching a Particular Exception 4.3.5 Catching All Exceptions 4.3.6 Reraising the Current Exception 4.3.7 Defining Epilogue Actions for a Block 4.3.8 Determining the Current Exception 4.3.9 Importing a System-Defined Error Status into the Program as an Exception 4.3.10 Exporting a System-Defined Error Status 4.3.11 Reporting an Exception 4.3.12 Determining Whether Two Exceptions Match 4.4 C Language Syntax 4.5 Rules and Conventions for Modular Use of Exceptions 4.6 Pthread Exceptions and Definitions 5 DECthreads Examples 5.1 Prime Number Search Example 5.2 Asynchronous User Interface Example Part II Digital Proprietary Interface (cma) Reference cma_alert_disable_asynch cma_alert_disable_general cma_alert_enable_asynch cma_alert_enable_general cma_alert_restore cma_alert_test cma_attr_create cma_attr_delete cma_attr_get_guardsize cma_attr_get_inherit_sched cma_attr_get_mutex_kind cma_attr_get_priority cma_attr_get_sched cma_attr_get_stacksize cma_attr_set_guardsize cma_attr_set_inherit_sched cma_attr_set_mutex_kind cma_attr_set_priority cma_attr_set_sched cma_attr_set_stacksize cma_cond_broadcast cma_cond_create cma_cond_delete cma_cond_signal cma_cond_signal_int cma_cond_timed_wait cma_cond_wait cma_debug cma_debug_cmd cma_delay cma_handle_assign cma_handle_equal cma_init cma_key_create cma_key_get_context cma_key_set_context cma_lock_global cma_mutex_create cma_mutex_delete cma_mutex_lock cma_mutex_try_lock cma_mutex_unlock cma_once cma_stack_check_limit_np cma_thread_alert cma_thread_create cma_thread_detach cma_thread_exit_error cma_thread_exit_normal cma_thread_get_priority cma_thread_get_sched cma_thread_get_self cma_thread_join cma_thread_set_priority cma_thread_set_sched cma_time_get_expiration cma_unlock_global cma_yield Part III DECthreads Library Reference cma_lib_attr_create cma_lib_attr_delete cma_lib_attr_get_queuesize cma_lib_attr_set_queuesize cma_lib_queue_create cma_lib_queue_delete cma_lib_queue_dequeue cma_lib_queue_enqueue cma_lib_queue_requeue cma_lib_queue_try_dequeue cma_lib_queue_try_enqueue cma_lib_queue_try_requeue cma_lib_queue_try_enqueue_int Part IV POSIX 1003.4a Reference pthread_attr_create pthread_attr_delete pthread_attr_getinheritsched pthread_attr_getprio pthread_attr_getsched pthread_attr_getstacksize pthread_attr_setinheritsched pthread_attr_setprio pthread_attr_setsched pthread_attr_setstacksize pthread_cancel pthread_cleanup_pop pthread_cleanup_push pthread_condattr_create pthread_condattr_delete pthread_cond_broadcast pthread_cond_destroy pthread_cond_init pthread_cond_signal pthread_cond_timedwait pthread_cond_wait pthread_create pthread_delay_np pthread_detach pthread_exit pthread_get_expiration_np pthread_getprio pthread_getscheduler pthread_getspecific pthread_join pthread_keycreate pthread_lock_global_np pthread_mutexattr_create pthread_mutexattr_delete pthread_mutexattr_getkind_np pthread_mutexattr_setkind_np pthread_mutex_destroy pthread_mutex_init pthread_mutex_lock pthread_mutex_trylock pthread_mutex_unlock pthread_once pthread_self pthread_setasynccancel pthread_setcancel pthread_setprio pthread_setscheduler pthread_setspecific pthread_testcancel pthread_unlock_global_np pthread_yield A Considerations for Systems Based on UNIX Software A.1 Calling Services A.1.1 Jacket Routines A.1.1.1 The fork( ) Jacket Routine A.1.1.2 The atfork( ) Jacket Routine A.1.1.3 Compiling Code with Jacketed System Calls A.2 Using Signals A.2.1 Types of Signals A.2.1.1 Terminating Signals A.2.1.2 Nonterminating Signals A.2.1.3 Synchronous Signals A.2.1.4 Asynchronous Signals A.2.2 DECthreads Signal Handling A.2.2.1 The POSIX sigwait Service A.2.2.2 The POSIX sigaction Service A.2.3 Alternatives to Using Signals A.3 Software Interrupts and Exceptions A.4 Signals Reported as Exceptions A.4.1 Synchronous Terminating Signals A.4.2 Arithmetic Error Signals A.4.3 Illegal Instruction Signals A.5 Debugging Threads B Considerations for VMS Systems B.1 Using DECthreads with Asynchronous System Trap (AST) Routines B.2 Declaring a VMS Condition Handler B.3 Linking to VMS Images B.4 Including DECthreads Header Files B.5 Debugging Threads B.5.1 Calling the cma_debug Routine B.5.2 Using VMS Debugger Tasking Support C DECthreads Exceptions D DECthreads Nonstandard Types for the POSIX 1003.4a Interface EXAMPLES 5-1 C Program Example (Prime Number Search) 5-2 C Program Example (Asynchronous User Interface) FIGURES 1-1 Single Threaded Process 1-2 Multithreaded Process 1-3 Thread State Transition Diagram 1-4 Work Crew Model of Thread Operation 1-5 Pipelining Model of Thread Operation 2-1 Only One Thread Can Lock a Mutex 2-2 Thread A Waits on Condition Ready 2-3 Thread B Signals Condition Ready 2-4 Thread A Wakes and Proceeds 2-5 Flow with FIFO Scheduling 2-6 Flow with RR Scheduling 2-7 Flow with Default Scheduling TABLES 1 Conventions 4-1 Pthread Exceptions A-1 Synchronous Terminating Signals A-2 Arithmetic Error Signals A-3 Illegal Instruction Signals A-4 DECthreads Debugging Commands and Qualifiers B-1 DECthreads Header Files B-2 DECthreads Debugging Commands and Qualifiers C-1 DECthreads Exceptions D-1 DECthreads Nonstandard Data Types