Correct way to access data once a thread is done with events

Hi All,

So I’m working on a project that pings a CMS and parses through all the data. I’m doing this bit in a thread because the XML returned from the CMS is quite long and parsing through it causes a lag on my front-end code. Here’s my thread code :

  
  
  
void CMS_EventData::threadedFunction( )   
{  
	  
    bool bResult = false ;   
  
	while(isThreadRunning() != 0)   
    {    
       if(lock())   
       {    
			float startTime = ofGetElapsedTimef() ;   
			bResult = initXmlData() ;   
                        unlock() ;   
		        stopThread( ) ;   
			ofSleepMillis(30);   
		  
			cout << "parsed threaded in " << ofGetElapsedTimef() - startTime << " result of : " << bResult << endl ;   
       }        
    }  
  
     ofLogError( "XML PARSE COMPLETE" ) << "notifying event with result of : " << bResult << " for event : " << title << endl ;   
	 if ( bResult == true )   
		 ofNotifyEvent( CMS_Events::Instance()->THREADED_XML_PARSE_COMPLETE , title ) ;   
	 else  
		 ofLogError( "XML DID NOT LOAD PROPERLY" ) ;  
}  
  

So the XML loads and then it dispatches an event letting me know it is fully loaded and ready to go. In my testApp this is the code that’s handling the event :

  
  
void testApp::xmlParseCompleteHandler( string &args )   
{  
	ofLogError( "XML PARSE COMPLETE " ) ;  
	parseDataDelayArgs = 0.0f;   
	Tweenzor::add( &parseDataDelayArgs , 0.0f , 1.0f , 0.0f , 0.00001f ) ;   
	Tweenzor::addCompleteListener( Tweenzor::getTween( &parseDataDelayArgs) , this , &testApp::parseDataDelayHandler ) ;   
}  
  
void testApp::parseDataDelayHandler( float * args )    
{  
	bXmlReady = true ;   
	ofLogError("  testApp::parseDataDelayHandler " ) ;   
	loadEventAssets( currentEvent );  
}  
  

so while the thread loads and does everything I want it too, when I try to parse the event data the call stack indicates that my app still thinks it’s in a thread ( all the way back to threadedFunction() ) so I can’t initialize any openGL calls like FBO etc. Does the event dispatch happen immediately ? I always assumed it happened the frame after, but maybe I’m just mistaken.

So my hacky solution was to just tween something over a small duration and in that complete listener then parse the data. I *know* there is a better way to do this I’m just missing a step. Any pointers would be radsauce.

yes an event is triggered from the thread it’s notified.

the way to do this is to put the results in a queue from the thread and then in the main thread in update check if the queue has any results, get them and remove them.

don’t forget to lock while accessing the queue since you are accessing from different threads.

Gotcha ! That makes sense , I had always assumed event dispatching was asynchronous or queued automatically. Thanks for everything Arturo :slight_smile: