[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

C++ throw in a thread

Is there a way to allow C++ code to throw in a thread?  I compiled the
following code as follows and it aborts due to the throw.

bash-2.05$ throwtest
throwing check



g++ -g throwtest.cpp -othrowtest -lpthread

#define _GNU_SOURCE
#define _REENTRANT
#define MAX 5
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>
using namespace std;

void * throwit(void *);

int main(int argc, char *argv[]) {
   pthread_t t;
   int i;
   int status;
      if( pthread_create(&t,NULL,throwit,(void *)i) != 0) {
        cerr << "pthread_create failed" << endl;
        return 1;
      pthread_join(t, (void **)&status);
      cout << "end of thread " << endl;
      cout << "unexpected exception" << endl;
void * throwit(void * t_id) {
   int i;
   float f = 4.0;
   for (i=0;i<MAX; i++) {
      try {
         cout<<"throwing check"<<endl;
         throw "check";
      catch(char * st) {
         cout << "throwit caught" << st << endl;
   return 0;

This message is intended only for the personal and confidential use of the designated recipients named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation to buy any financial product or service, or as an official confirmation of any transaction, or as an official statement of Moneyline Telerate.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.

Reply to: