Thread-safetyness of exceptions Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
namo
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Apr 19, 2007 4:38 pm
Location: /dev/null

Thread-safetyness of exceptions

Post by namo »

Is it safe to use exceptions within threads?
"It is like doing a foo to bar over a double baz, backwards." -Ymer
megabyte
I live to help wx-kind
I live to help wx-kind
Posts: 196
Joined: Tue Dec 07, 2004 8:54 pm
Location: Essen, Germany

Post by megabyte »

What do you mean "safe" or "does not safe"? Which exceptions behavior do you expect?

You can use it in you threads, but exception thrown in a thread will not affect on other ones.
namo
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Apr 19, 2007 4:38 pm
Location: /dev/null

Post by namo »

Code: Select all

void CThread::Entry(void)
{
  try
  {
    printf("%i running in %p\n", wxThread::GetCurrentId(), this);
    wxThread::Sleep(3000);
    printf("%p %i throwing %s\n", this, wxThread::GetCurrentId(), "foo");
    throw "foo";
  }
  catch(const char* psz)
  {
    printf("%p %i catching %s\n", this, wxThread::GetCurrentId(), psz);
  }
}
I have two instances of wxThread-derived classes loaded and I when start both of them roughly at the same time I got this output:

Code: Select all

3256 running in 025249B8
3816 running in 02524AB0
025249B8 3256 throwing foo
02524AB0 3256 catching foo
So when two threads are in the same function and one throws an exception I catch it in the same thread but with the wrong instance of the class.

But then the threads execute in different functions everything works like it should.
"It is like doing a foo to bar over a double baz, backwards." -Ymer
Game_Ender
Knows some wx things
Knows some wx things
Posts: 45
Joined: Wed Jun 15, 2005 5:47 pm

Post by Game_Ender »

Threads by definition have different stacks and if I remeber my exception handling right (at least on linux and GCC) exceptions roll back the stack as they attempt to be caught. So this should happen independently in each thread.

Can you post a more complete example.
megabyte
I live to help wx-kind
I live to help wx-kind
Posts: 196
Joined: Tue Dec 07, 2004 8:54 pm
Location: Essen, Germany

Post by megabyte »

I entered your thread code to a function of the console sample. It looks like

Code: Select all

wxThread::ExitCode MyDetachedThread::Entry()
{
	try
	{
		printf("%i running in %p\n", wxThread::GetCurrentId(), this);
		wxThread::Sleep(3000);
		printf("%p %i throwing %s\n", this, wxThread::GetCurrentId(), "foo");
		throw "foo";
	}
	catch(const char* psz)
	{
		printf("%p %i catching %s\n", this, wxThread::GetCurrentId(), psz);
	}     {
        wxCriticalSectionLocker lock(gs_critsect);
        if ( gs_counter == (size_t)-1 )
            gs_counter = 1;
        else
            gs_counter++;
    }

    for ( size_t n = 0; n < m_n; n++ )
    {
        if ( TestDestroy() )
        {
            m_cancelled = true;

            break;
        }

        wxPutchar(m_ch);
        fflush(stdout);

        wxThread::Sleep(100);
    }

    return 0;
}
The output is
*** Testing detached threads ***
336 running in 009569D8
2548 running in 00958140
880 running in 00956980
009569D8 336 throwing foo
00958140 2548 throwing foo
00956980 880 throwing foo
009569D8 336 catching foo
00958140 2548 catching foo
00956980 880 catching foo
BCABCABCABCABCABCABCABCABCABCA
Can you do the same modification on your pc? Do you have similar output? If yes, then please post the threads declaration and creation code.

BTW, the original wxThread::Entry methods returns wxThread::ExitCode (decared as void *).
namo
In need of some credit
In need of some credit
Posts: 8
Joined: Thu Apr 19, 2007 4:38 pm
Location: /dev/null

Post by namo »

I found why it failed... A DLL I linked with was not build with -mthreads...

But still thanks for your help.
"It is like doing a foo to bar over a double baz, backwards." -Ymer
Post Reply