I ran into a problem catching the std :: invalid_argument exception, which I cannot track. I am using gcc 4.4.0 (windows), pthreads-win32 2.8.0 GC2 DLL.
Basically, from two threads (the main thread and the one starting with pthread_create), I am trying to create an instance of class A at about the same time. The constructor throws std :: invalid_argument, but it is surrounded by try / catch blocks that should catch the exception. However, this does not happen (very rarely, only one of the threads can catch an exception - there is no rule as to what will do this, though)
If I try to create an object for only one of the threads, the creation will work as it should, and an exception will be detected. If I create two objects at different times, the creation works as it should, and the exception is caught. If I try to create them at the same time, :: terminate () is called.
Maybe someone has an idea why this is happening (I excluded the headers):
void *run(void *ptr) { Sleep(5000); try { A *a = new A(5); a->a = 12; } catch (std::exception &ex) { printf("t - %s\n", ex.what()); } return NULL; } int main(void) { pthread_t t; if (pthread_create(&t, NULL, run, NULL) != 0) { printf("No thread\n"); } else { Sleep(5000); try { A *a = new A(5); a->a = 13; } catch (std::exception &ex) { printf("M - %s\n", ex.what()); } pthread_join(t, NULL); } return 0; } class A { public: A(int a); virtual ~A(); int a; }; A::A(int a) { throw std::invalid_argument("Invalid!"); } A::~A(){}
Makefile:
CXXFLAGS = -O0 -g -Wall -Werror -fmessage-length=0 OBJS = WOpenTest.o Ao INCL = -I../pthreads-win32/include LIBS = -lws2_32 -lgdi32 -lpthreadGC2 LIB_DIRS = -L ../pthreads-win32/lib TARGET = WOpenTest.exe $(TARGET): $(OBJS) $(CXX) -o $(TARGET) $(OBJS) $(LIBS) $(LIB_DIRS) $(INCL) WOpenTest.o : WOpenTest.cpp g++ $(CXXFLAGS) -c WOpenTest.cpp $(INCL) Ao : A.cpp Ah g++ $(CXXFLAGS) -c A.cpp $(INCL) all: $(TARGET) clean: rm -f $(OBJS) $(TARGET)
The output that I see is:
(Most often) $. / WOpenTest.exe
This application requested Runtime to finish it in an unusual way. Please contact for more information.
This application requested Runtime to finish it in an unusual way. Please contact for more information. call termination after throwing an instance of 'std :: invalid_argument' termination is called recursively
or
$. / WOpenTest.exe
This application requested Runtime to finish it in an unusual way. Please contact for more information. M - Wrong!
or
$. / WOpenTest.exe
This application requested Runtime to finish it in an unusual way. Please contact for more information. t - Wrong!
or
This application requested Runtime to finish it in an unusual way. Please contact for more information.
This application requested Runtime to finish it in an unusual way. Please contact for more information. end of call after throwing instance 'std :: invalid_argument'
what (): Wrong!
Any ideas on what I should do, but I don't? Or is something missing from pthreads?