Multi-Thread Application problem

Benjamin Schulte Aldoric at gmx.de
Mon Feb 4 12:32:44 PST 2008


Hi,

thanks for your reply. I guess this was the fact I didn't know so far. Is it for the constructor equal?

But yes, I had a sleep in the deconstructor, waiting for the other thread. I then just wonder why it worked sometimes and only a few times it didn't. But maybe there's still another bug in my applcation.

But here's the code - maybe there's still a way to optimize it. But I guess first I need to replace the deconstructor with a free-method deleting itself after freeing the resources.

ThreadCall.doEvents( ) is being called from Thread A.



class someclass {
~this( )
{
	// Outside-Of-Thread call
	ThreadCall.call( function( uint id ) {

		glDeleteBuffersARB( 1, cast(uint*)&id );
		return 0;

	}, cast(uint)vboBuffer );
}
}





// Alias
alias int function( uint ) ThreadCallPtr;

/*********************************************************************
 * The thread call class
*********************************************************************/
class ThreadCall
{
	/*********************************************************
	 * Call a method
	*********************************************************/
	static uint call( ThreadCallPtr ptr, uint param )
	{
		uint myId = getUniqueId( );

		// If myId is equal to the main thread ID, we don't
		// have to wait until we can call this method
		if( myId == mainThreadId )
		{
			// Call method
			return ptr( param );
		}
		else
		{
			// Well, we have to wait for the main thread

			// Create call
			
			ThreadCall c = new ThreadCall;
			c.method     = ptr;
			c.param      = param;
			
			// Append call to event list
			mainSemaphore.enter( );
			calls ~= c;
			mainSemaphore.leave( );

			// Wait for call to be finished	
			while( !c.finish ) usleep(10);

			// Delete my item and return its result
			int r = c.result;
			delete c;
			
			return r;
		}
	}
	
	/*********************************************************
	 * Do all events (Called in the main thread)
	*********************************************************/
	static void init( )
	{
		// Get main-thread ID
		mainThreadId  = getUniqueId( );
		mainSemaphore = new Semaphore( );
	}
	
	/*********************************************************
	 * Return an unique ID describing the current thread
	 * WARNING: OS DEPENDEND!	 
	*********************************************************/
	static uint getUniqueId( )
	{
		// Windows method
		return cast(uint)GetCurrentThreadId( );	
	}

	/*********************************************************
	 * Do all events (Called in the main thread)
	*********************************************************/
	static void doEvents( )
	{
		mainSemaphore.enter( true );
		
		// Go through every item
		foreach( ThreadCall c; calls )
		{
			// Call method
			c.result = c.method( c.param );
			c.finish = true;
		}

		// Clear list, cause we did all
		calls.length = 0;		
		mainSemaphore.leave( );
	}

	/********************************************************/
	ThreadCallPtr		method;
	uint				param;
	int					result;
	bit					finish = false;
	
	static ThreadCall calls[];
	static uint       mainThreadId;
	static Semaphore  mainSemaphore;
};




More information about the Digitalmars-d mailing list