upgrading to wx-2.9.1: non-gui (wxbase) thread not running. 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
pervulgo
Earned a small fee
Earned a small fee
Posts: 19
Joined: Mon Nov 13, 2006 10:49 am

upgrading to wx-2.9.1: non-gui (wxbase) thread not running.

Post by pervulgo » Mon Mar 07, 2011 8:29 am

Hello,

I'm upgrading my project to wx-2.9.1. I have got my project compiling again but it does not run correctly (yet). It seems that the threads in my program never reach their entry point. My program is a console, non-gui application. I have made a simple sample program that demonstrates this thread problem. Perhaps I need to initialize wxWidgets-2.9.1 in a different way since wxBase now also supports events ?

The sample program runs fine under windows/mingw and linux with wx-2.8.11 but it does not run under windows/mingw and linux with wx-2.9.1

Here is the sample:

Code: Select all

#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

class TestThread : public wxThread
{
  public:
     TestThread()
		: wxThread(wxTHREAD_DETACHED)
     {
		wxLogMessage("TestThread : Constructor.");
     }
  protected:
     virtual void* Entry()
     {
		//The entry point is not reached
		wxLogMessage("TestThread : thread entry.");
                return NULL;
     }
}; 


int main( int argc, char* argv[] )
{
	//
	// Initialize the wxWidgets library
	//
	wxInitializer initializer(argc, argv);
	if ( !initializer.IsOk() )
	{
		wxLogError("Failed to initialize the wxWidgets library, aborting.");
		return 1;
	}

	wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
	
	wxLogMessage("Main thread.");
	
	//Create and run the TestThread
	TestThread* tThread = new TestThread();
	 
	if ( tThread->Create() != wxTHREAD_NO_ERROR )
	{
		wxLogError( _T("Cannot create thread!") );
	}
	else
	{
		if ( tThread->Run() != wxTHREAD_NO_ERROR )
		{
			wxLogError( _T("!!Cannot run thread!") );
		}
	}
	
	//Sleep a bit to allow for the TestThread to run
	::wxMilliSleep(5000);
	
	wxLogMessage("Exiting main app");
	
	return 0;
}

The output with wx-2.8.11:

09:02:59: Main thread.
09:02:59: TestThread : Constructor.
09:02:59: TestThread : thread entry.
09:03:04: Exiting main app

The output with wx-2.9.1:

9:03:57 AM: Main thread.
9:03:57 AM: TestThread : Constructor.
9:04:02 AM: Exiting main app

The thread entry is not reached :-(

Any help is appreciated,
best regards,

Peter.

RainRat
I live to help wx-kind
I live to help wx-kind
Posts: 178
Joined: Thu Jan 06, 2011 11:26 pm

Post by RainRat » Mon Mar 07, 2011 9:25 am

I believe that the wxLogMessage in the Entry() function is simply not being output because it is executed in the other thread rather than the main thread. I compiled this on MSVC2010 with 2.9.1 and got the same output as you (no thread entry output), but with a breakpoint I verified that the thread Entry() function was reached and a separate thread was running.

Stepping into the wxLogMessage function, it appears that there is a check for whether "thread logging" is enabled, so maybe this was changed in 2.9.1.

okurtsev
Experienced Solver
Experienced Solver
Posts: 54
Joined: Mon Jan 25, 2010 3:26 pm
Location: Ukraine, Kiev

Post by okurtsev » Mon Mar 07, 2011 9:42 am

Probably it is because wxLogMessage behaviour was changed in 2.9.1

http://docs.wxwidgets.org/2.9/overview_log.html -

"Starting with wxWidgets 2.9.1, logging functions can be safely called from any thread. Messages logged from threads other than the main one will be buffered until wxLog::Flush() is called in the main thread (which usually happens during idle time, i.e. after processing all pending events)"
C++, Win XP-32, Win7-64, WinVista-32, MS VS 2005, 2008, 2010

pervulgo
Earned a small fee
Earned a small fee
Posts: 19
Joined: Mon Nov 13, 2006 10:49 am

Post by pervulgo » Mon Mar 07, 2011 10:46 am

Thanks for your quick replies!

The sample indeed runs fine, it is only that the wxLogMessage() was not shown. Adding a wxLog::FlushActive() as suggested in the main thread indeed shows the message.

My program still has a problem, I assumed the problem was thread related (because I saw no messages) but this appears not te be the case. (Feeling a bit silly now). I'm using wxSockets, so probably the problem lies there. The wxSocket implementation was revised in wx-2.9.1 if I'm correct ? I'll try to isolate the problem. Thanks for the help.


Cheers,

Peter.

Post Reply