B Using EMS together with POSIX threads

When built using the –with-pthreads configuration option, which is the default on platforms supporting POSIX threads, EMS can be used in a threaded application or library. The only requirement is that the sections of the application that use threads are wrapped in emsMark/emsRlse blocks, as in:

  void *make_report( void *threadid )
      int tid;
      int status = SAI__ERROR;
      tid = (int) threadid;
      emsSeti( "THREADID", tid );
      emsRep( "THREAD", "Oh no an error in thread ^THREADID", &status );
      return NULL;
  int main ( int argc, char *argv[] )
      pthread_t threads[ NUM_THREADS ];
      int rc;
      int t;
      int status = SAI__OK;
      for ( t = 0; t < NUM_THREADS; t++ ) {
          pthread_create( &threads[ t ], NULL, make_report, (void *)t );
      /*  Wait for threads to complete. */
      for ( t = 0; t < NUM_THREADS; t++ ) {
          pthread_join( threads[ t ], NULL );
      /* Check for the exit status */
      emsStat( &status );

The error messages can then be delivered or loaded as normal (when using ERR it is important not to make any calls to it in the threaded section, as this may access unprotected global data). The status return recovered by emsStat will be one from one of the threads, it is not possible to find out the status from all threads using EMS. In a standalone program as shown the error messages:

  !! Oh no an error in thread 1
  !  Oh no an error in thread 2
  !  Oh no an error in thread 3
  !  Oh no an error in thread 4

will be shown using the default delivery printf mechanism during the emsRlse call (since the error stack context falls to the base level).