public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Are exception with gcc 2.95.2 thread-safe?
@ 2000-03-21  3:47 aurelien.cornet
  2000-03-22 11:04 ` Dima Volodin
  0 siblings, 1 reply; 4+ messages in thread
From: aurelien.cornet @ 2000-03-21  3:47 UTC (permalink / raw)
  To: gcc

[-- Attachment #1: Type: text/plain, Size: 773 bytes --]

The usual answer to this question is: 

The ANSI/ISO C++ Professional Programmer's Handbook (Danny Kalev) 
"In a multi-threading environment, exception handling should be
thread-safe, but a single threading environment
can implement exception handling in an non-thread-safe manner; this is
implementation-dependent issue." 

I try the attached progam on Linux, Sun, Aix:
In a main procedure, a try block creates a thread, sleep 2s (let the
thread to be run), then throw an exception. We expected that this
exception will be catched in main and the program terminates. But this
program will never terminate and the result is:
Thread has started 
Exception catched in thread 

Is it a bug? Can we use exception in a multi-threading program?

Best regards. 
Aurelien.
main.C


[-- Attachment #2: main.C --]
[-- Type: text/x-c, Size: 493 bytes --]

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#include <iostream.h>

void * thread_f (void *) {
  try {
    cout << "Thread has started " << endl;
    for (;;) sleep (10000);

  } catch (int i) {
    cout << "Exception catched in thread" << endl;
  }
}

int main () {
  try {
    pthread_t thread;
    pthread_create (&thread, 0, thread_f, 0);

    sleep (2);
    throw 10;

  } catch (int i) {
    cout << "exception catched in main" << endl;
  }
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Are exception with gcc 2.95.2 thread-safe?
  2000-03-21  3:47 Are exception with gcc 2.95.2 thread-safe? aurelien.cornet
@ 2000-03-22 11:04 ` Dima Volodin
  0 siblings, 0 replies; 4+ messages in thread
From: Dima Volodin @ 2000-03-22 11:04 UTC (permalink / raw)
  To: aurelien.cornet; +Cc: gcc

On Tue, 21 Mar 2000 12:46:52 +0100, you wrote:

>The usual answer to this question is: 
>
>The ANSI/ISO C++ Professional Programmer's Handbook (Danny Kalev) 
>"In a multi-threading environment, exception handling should be
>thread-safe, but a single threading environment
>can implement exception handling in an non-thread-safe manner; this is
>implementation-dependent issue." 
>
>I try the attached progam on Linux, Sun, Aix:
>In a main procedure, a try block creates a thread, sleep 2s (let the
>thread to be run), then throw an exception. We expected that this
>exception will be catched in main and the program terminates. But this
>program will never terminate and the result is:
>Thread has started 
>Exception catched in thread 
>
>Is it a bug? Can we use exception in a multi-threading program?

What compiler version and what compiler flags did you use? I tried your
program with g++ 2.95.2 in Solaris on both i386 and sparc and the output
in both cases was

	Thread has started
	exception catched in main

In both cases, the compiler was configured with --enable-threads=posix
and the program compiled as

	g++ -D_REENTRANT main.C -lpthread

The canonical 2.95.2 Solaris sparc configuration uses these flags
automatically if you specify -pthreads in the command line.

BTW, I use exceptions in multi-threaded environment pretty heavily. My
only gripe is that exceptions don't propagate across Solaris libraries
calls (I wonder if it's possible to recompile all of them with gcc when
the sources become available). Other than that everything works the way
it's expected to work.

>Aurelien.

Cheers!

Dima

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Are exception with gcc 2.95.2 thread-safe?
  2000-03-21 14:57 Loren James Rittle
@ 2000-03-29  1:19 ` aurelien.cornet
  0 siblings, 0 replies; 4+ messages in thread
From: aurelien.cornet @ 2000-03-29  1:19 UTC (permalink / raw)
  To: Loren James Rittle, Dima Volodin; +Cc: gcc

With my administrator, we try:
On Sun and Linux "configure --enable-threads" => The program is
operating
On Aix 4.3.2 "configure --enable-threads" => The program was not
operating. But using "configure --enable-threads=posix" the program is
now operating.

I can answer my question: YES.

Thank you very much for your help.

Aurelien.

----------------------------------------------------------------------
Message written by Loren James Rittle
> 
> > I try the attached progam on Linux, Sun, Aix:
> > In a main procedure, a try block creates a thread, sleep 2s (let the
> > thread to be run), then throw an exception. We expected that this
> > exception will be catched in main and the program terminates. But this
> > program will never terminate and the result is:
> > Thread has started
> > Exception catched in thread
> 
> > Is it a bug? Can we use exception in a multi-threading program?
> 
> Your program is operating as it should in the three environments I
> have easy access to: Solaris, Linux and FreeBSD.  I suspect that there
> is a reason why it isn't working for you.
> 
> Unfortunately, when you configure gcc, you have to explicitly inform
> the process that you want thread safety!  E.g.:
> 
> ../egcs/configure --enable-threads
> 
> Then, you have to provide a non-portable-between-gcc-based-
> development-environments switch during both compilation and linking:
> 
> On Linux and FreeBSD:
> 
> g++ -pthread main.C
> 
> On Solaris:
> 
> g++ -pthreads main.C
> 
> On OSF:
> 
> EITHER g++ -pthread main.C OR g++ -threads main.C
> (but note that depending on which switch you give, your program will
> link against a different set of libraries)
> 
> Don't ask me why you need to use different options... ;-)
> 
> Regards,
> Loren

----------------------------------------------------------------------
Message writen by Dima Volodin

> >The usual answer to this question is:
> >
> >The ANSI/ISO C++ Professional Programmer's Handbook (Danny Kalev)
> >"In a multi-threading environment, exception handling should be
> >thread-safe, but a single threading environment
> >can implement exception handling in an non-thread-safe manner; this is
> >implementation-dependent issue."
> >
> >I try the attached progam on Linux, Sun, Aix:
> >In a main procedure, a try block creates a thread, sleep 2s (let the
> >thread to be run), then throw an exception. We expected that this
> >exception will be catched in main and the program terminates. But this
> >program will never terminate and the result is:
> >Thread has started
> >Exception catched in thread
> >
> >Is it a bug? Can we use exception in a multi-threading program?
> 
> What compiler version and what compiler flags did you use? I tried your
> program with g++ 2.95.2 in Solaris on both i386 and sparc and the output
> in both cases was
> 
>         Thread has started
>         exception catched in main
> 
> In both cases, the compiler was configured with --enable-threads=posix
> and the program compiled as
> 
>         g++ -D_REENTRANT main.C -lpthread
> 
> The canonical 2.95.2 Solaris sparc configuration uses these flags
> automatically if you specify -pthreads in the command line.
> 
> BTW, I use exceptions in multi-threaded environment pretty heavily. My
> only gripe is that exceptions don't propagate across Solaris libraries
> calls (I wonder if it's possible to recompile all of them with gcc when
> the sources become available). Other than that everything works the way
> it's expected to work.
> 
> >Aurelien.
> 
> Cheers!
> 
> Dima

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Are exception with gcc 2.95.2 thread-safe?
@ 2000-03-21 14:57 Loren James Rittle
  2000-03-29  1:19 ` aurelien.cornet
  0 siblings, 1 reply; 4+ messages in thread
From: Loren James Rittle @ 2000-03-21 14:57 UTC (permalink / raw)
  To: aurelien.cornet; +Cc: gcc

> I try the attached progam on Linux, Sun, Aix:
> In a main procedure, a try block creates a thread, sleep 2s (let the
> thread to be run), then throw an exception. We expected that this
> exception will be catched in main and the program terminates. But this
> program will never terminate and the result is:
> Thread has started 
> Exception catched in thread 

> Is it a bug? Can we use exception in a multi-threading program?

Your program is operating as it should in the three environments I
have easy access to: Solaris, Linux and FreeBSD.  I suspect that there
is a reason why it isn't working for you.

Unfortunately, when you configure gcc, you have to explicitly inform
the process that you want thread safety!  E.g.:

../egcs/configure --enable-threads

Then, you have to provide a non-portable-between-gcc-based-
development-environments switch during both compilation and linking:

On Linux and FreeBSD:

g++ -pthread main.C

On Solaris:

g++ -pthreads main.C

On OSF:

EITHER g++ -pthread main.C OR g++ -threads main.C
(but note that depending on which switch you give, your program will
link against a different set of libraries)

Don't ask me why you need to use different options... ;-)

Regards,
Loren

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2000-03-29  1:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-21  3:47 Are exception with gcc 2.95.2 thread-safe? aurelien.cornet
2000-03-22 11:04 ` Dima Volodin
2000-03-21 14:57 Loren James Rittle
2000-03-29  1:19 ` aurelien.cornet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).