[omniORB] Sun C++ 5 or 6 "throw" is not thread safe.

Sai-Lai Lo S.Lo@uk.research.att.com
16 Jun 2000 12:45:16 +0100


Unfortunately, things are not as simple as it first looks.
I changed the test program to use pthread and corrected some obvious
mistakes and it no longer SEGV.

My omniorb test program still SEGV in exception unwinding though.

Sai-Lai


The revised test is as follows:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>

//#define SAFE

extern "C" void* do_it(void*);
int *now;

#ifdef SAFE
pthread_mutex_t throw_mut;
#endif

int
main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: throw numthreads\n");
    exit(1);
  }

  int numthreads = atoi(argv[1]);

#ifdef SAFE
  pthread_mutex_init(&throw_mut,NULL);
#endif

  now = new int[numthreads];
  for(int i = 0; i<numthreads; i++) {
    now[i] = 0;
    pthread_t id;
    pthread_create(&id,NULL,do_it,(void*)i);
  }

  while(1) {
    struct timeval to = {0, 10000};
    if (select(0, 0, 0, 0, &to) == -1) {
      perror("select");
      exit(1);
    }
    for (int i=0; i<numthreads; i++)
      now[i] = 1;
    }	
  
  return(0);
}	

class Excp {
public:
  Excp() {}
};

void*
do_it(void* arg) {

  int me = (int) arg;
  while(1) {
    try {
      while (1) {
	if (now[me] == 1) {
 #ifdef SAFE
	  pthread_mutex_lock(&throw_mut);
 #endif
	  throw(Excp());
	}
      }
    } catch (Excp &x) {
      now[me] = 0;
 #ifdef SAFE
      pthread_mutex_unlock(&throw_mut);
 #endif
    }
  }
  return 0;
}




-- 
Sai-Lai Lo                                   S.Lo@uk.research.att.com
AT&T Laboratories Cambridge           WWW:   http://www.uk.research.att.com 
24a Trumpington Street                Tel:   +44 1223 343000
Cambridge CB2 1QA                     Fax:   +44 1223 313542
ENGLAND