Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » MultiThreads and GLCtrl
MultiThreads and GLCtrl [message #28735] Tue, 14 September 2010 15:39 Go to next message
281264 is currently offline  281264
Messages: 272
Registered: June 2010
Location: Spain
Experienced Member
Hi,

I am interested in an example about how to use GLCtrl and multithread in U++. Is there anything (example) already done? If not, some hints would be great.

Thank you.

Cheers,

Javier
Re: MultiThreads and GLCtrl [message #28776 is a reply to message #28735] Thu, 16 September 2010 10:36 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
I believe that OpenGL requires all calls to be made from the main thread, so synchronisation will be necessary.
Re: MultiThreads and GLCtrl [message #28806 is a reply to message #28735] Fri, 17 September 2010 14:36 Go to previous messageGo to next message
ratah is currently offline  ratah
Messages: 107
Registered: July 2010
Experienced Member
Hi,

Here is an example of multithread usage.
It's a copy of code posted here (or in the tutorial of upp, i do not remember). I give it a minor modification.

I have a bug: when you push the STOP button, it crash!

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

class TestThread : public TopWindow
{
	protected:
		bool stop;
		void buttonCb(void);
		Thread thr1, thr2;
		virtual void thrCb(void);
		virtual void thrPerso(void);
		
		ProgressIndicator progress;
		int32 icounter;
		Button button;
		StatusBar status;
		
	public:
		typedef TestThread CLASSNAME;

		TestThread();

};

void TestThread::thrCb(void)
{
	for(;;)
	{
		
		GuiLock __;
			
		if(progress < 100)
			progress++;
		else
			progress = 0;
		if(stop)
			break;

		Sleep(100);
	}
}

void TestThread::thrPerso(void)
{
	for(; ; )
	{
		{			
			GuiLock __;
			icounter++;
			status.Set(IntStr(icounter));
		}
		Sleep(100);
	}
}

void TestThread::buttonCb(void)
{
	if(stop)
	{
		stop = false;
		button.SetLabel("STOP");

          // I run here the first thread
		thr1.Run(THISBACK(thrCb));

          // I run here the second thread
		thr2.Run(THISBACK(thrPerso));
	}
	else
	{
		stop = true;
		thr1.Wait();
		thr2.Wait();
		button.SetLabel("START");
	}
	
	
}

TestThread::TestThread()
{
	SetRect(0, 0, 300, 150);
	Add(button);
	button.TopPos(90, 30).HCenterPos(100);
	button.SetLabel("START");
	Add(progress);
	progress.TopPos(30, 40).HCenterPos(250);
	progress.Set(0, 100);
	icounter = 0;
	AddFrame(status);
	status.Set(" ");

	stop = true;
	button <<= THISBACK(buttonCb);
		
}

GUI_APP_MAIN
{
	TestThread().Run();
}


Do not forget to change Configuration flags to Executable, GUI, Multithreaded!!

Hope you can fix the bug, good luck

RAtah
Re: MultiThreads and GLCtrl [message #28809 is a reply to message #28806] Fri, 17 September 2010 17:38 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
For one thing you aren't actually checking for stop in thrPerso.

The more fundamental problem is that calling Wait on a thread with GuiLock on it from the Gui thread won't work. The Gui thread is waiting for the thread to finish while the thread is waiting for the GUI thread to finish (or go idle). Therefor you get a lock.

I would suggest running a timer callback that checks for thread completion or something.
Re: MultiThreads and GLCtrl [message #28918 is a reply to message #28809] Sun, 26 September 2010 23:11 Go to previous message
281264 is currently offline  281264
Messages: 272
Registered: June 2010
Location: Spain
Experienced Member
Hi Ratah

After some readings on the multithreading topic, I have modified the application you sent. I think now it works as you are expecting; test it and tell me.

Cheers,

Javier
Previous Topic: TreeCtrl: How to...?
Next Topic: LoadFromFile, StoreToFile and Serialize
Goto Forum:
  


Current Time: Fri Nov 01 00:54:20 CET 2024

Total time taken to generate the page: 0.01779 seconds