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 » U++ Library support » U++ MT-multithreading and servers » Threading example for U++
Re: Threading example for U++ [message #10270 is a reply to message #10266] Thu, 28 June 2007 18:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
Good job Smile

Mirek
Re: Threading example for U++ [message #10294 is a reply to message #10199] Sat, 30 June 2007 12:48 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

I bit extened:
- More factal sets
- Logarythmic color scale
- one rendering method for all fractals
- fractal calculating methods are callbacks for thread rendering methods
- left down + mouse move , moves the fractal
- left down + CTRL + mouse move selects area for zoom
- axis describing complex area

[Updated on: Sun, 08 July 2007 01:13]

Report message to a moderator

Re: Threading example for U++ [message #10298 is a reply to message #10294] Sat, 30 June 2007 17:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
Starts to look really nice. It is not an example I am searching for 'reference', but it is now very nice demonstration example...

BTW, Rect_<double> is 'Rectf'. Rect_, Point_, Size_ were introduced only as implementation technique for Point, Point16, Pointf etc...
Re: Threading example for U++ [message #10299 is a reply to message #10298] Sat, 30 June 2007 18:09 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
BUG: When moving the area, if you move the mouse outside the view, it gets "stuck" and continues moving even if mouse button is released...

Instead of 'moving' flag, use SetCapture and IsCapture.

You might also try to make the program structure a little bit less complex, I have noticed the troubles you have with routing mouse messages.

One option is to "reverse" class-member and widget-parent-child relations. In U++, you can easily contain the main window IN the child ctrl class:

struct Threads : Ctrl {
    TopWindow win;
    
    void Paint(...);
    void LeftDown(...);

    Threads() { win.Add(*this); ... }
};


This would significantly reduce the number of line in the example (I think).
Re: Threading example for U++ [message #10301 is a reply to message #10299] Sat, 30 June 2007 18:26 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

[quote title=luzr wrote on Sat, 30 June 2007 18:09]BUG: When moving the area, if you move the mouse outside the view, it gets "stuck" and continues moving even if mouse button is released...

Instead of 'moving' flag, use SetCapture and IsCapture.
[/quote
I check this out.

[quote]
You might also try to make the program structure a little bit less complex, I have noticed the troubles you have with routing mouse messages.
[/quote]

I don't have troubles, i jut follow familiar for me .NET rules of connecting events, and similar to Qt. Control got events which can be connected, thats all.
I didn't use such techniques of letting sub control owning entire window. It don't looks good for me.
Control owns window, window contains control which contains this window. Smile Circular dependency ?
What in case when window got multiple controls and we want to connect many events ?


[Updated on: Sat, 30 June 2007 18:27]

Report message to a moderator

Re: Threading example for U++ [message #10302 is a reply to message #10301] Sat, 30 June 2007 20:17 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
Quote:


Quote:


You might also try to make the program structure a little bit less complex, I have noticed the troubles you have with routing mouse messages.



I don't have troubles, i jut follow familiar for me .NET rules of connecting events, and similar to Qt. Control got events which can be connected, thats all.
I didn't use such techniques of letting sub control owning entire window. It don't looks good for me.



That is just mind barrier, believe me. The key to understand is that class containment does not have to be related to the GUI containment.

Quote:


Control owns window, window contains control which contains this window. Smile Circular dependency ?
What in case when window got multiple controls and we want to connect many events ?



I think "got" is a wrong word here.... Smile Sure, nothing prevents you to follow GUI in class composition, but in U++ it is not requirement.

Before going deep into philosophic details, "inverse layout" in your example would very likely produce much less complex code - which is the thing I am usually pursuing...
Re: Threading example for U++ [message #10303 is a reply to message #10199] Sun, 01 July 2007 00:31 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
P.S.:

String FormatTS (int ms) { return Format("Render time %d:%02.2d.%03d",(ms/1000/60), (ms / 1000) % 60, ms % 1000 );}
Re: Threading example for U++ [message #10313 is a reply to message #10303] Sun, 01 July 2007 20:40 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

luzr wrote on Sun, 01 July 2007 00:31

P.S.:

String FormatTS (int ms) { return Format("Render time %d:%02.2d.%03d",(ms/1000/60), (ms / 1000) % 60, ms % 1000 );}



What's wrong with this ?
Re: Threading example for U++ [message #10314 is a reply to message #10313] Sun, 01 July 2007 21:15 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
Well, the original code showed one minute, 3 seconds and 50 miliseconds a 1:3:50 (if I remember well), while I would like to see 1:03.050.
Re: Threading example for U++ [message #10315 is a reply to message #10314] Sun, 01 July 2007 23:53 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

Ok.
Btw the Format example is not finished, i don't known how to format double value with strict number of decimal places.

I will next days improve a bit it for ex. many colorize sets etc.

like below.


index.php?t=getfile&id=644&private=0
index.php?t=getfile&id=645&private=0
  • Attachment: julia5.png
    (Size: 270.86KB, Downloaded 977 times)
  • Attachment: mandel5.PNG
    (Size: 199.69KB, Downloaded 965 times)

[Updated on: Mon, 02 July 2007 00:07]

Report message to a moderator

Re: Threading example for U++ [message #10318 is a reply to message #10315] Mon, 02 July 2007 08:27 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
arturbac wrote on Sun, 01 July 2007 17:53

Ok.
Btw the Format example is not finished, i don't known how to format double value with strict number of decimal places.



http://www.ultimatepp.org/src$Core$Format$en-us.html

Quote:


printf formatters

Most printf formatters are supported:

c d i o x X ld li lo lx lX e E f g G s

Please refer to printf documentation for the description.



Some more *user* notes about the example:

- option to remove the ruler
- mouse click / Ctrl should be IMO swapped (move with Ctrl, select without)
- 'go back' button

Mirek
Re: Threading example for U++ [message #10416 is a reply to message #10199] Sun, 08 July 2007 01:12 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

A bit extended
SSE2/x86 code for Mandelbrot & Julia

in readme.txt are performance of Code/Compiler test results very interesting, for BSD and Win

- new colorizing functions
- button - switch betwen zoom/move mode

etc.

(things Mirek write I will corect in future)



[Updated on: Wed, 18 July 2007 00:30]

Report message to a moderator

icon1.gif  Re: Threading example for U++ [message #10574 is a reply to message #10416] Tue, 17 July 2007 03:40 Go to previous messageGo to next message
nixnixnix is currently offline  nixnixnix
Messages: 415
Registered: February 2007
Location: Kelowna, British Columbia
Senior Member
should this code compile on a default install of 2007.1 under windows?

I get

----- plugin\z ( GUI MT GCC FORCE_SPEED BLITZ WIN32 ) (7 / 9)
adler32.c
plugin/bmp: 4 file(s) built in (0:01.67), 417 msecs / file, duration = 3500 msecs, parallelization 100%
compress.c
crc32.c
deflate.c
gzio.c
infblock.c
infcodes.c
inffast.c
inflate.c
inftrees.c
infutil.c
trees.c
uncompr.c
zutil.c
----- plugin\png ( GUI MT GCC FORCE_SPEED BLITZ WIN32 ) (8 / 9)
pnglib.c
plugin\z: 14 file(s) built in (0:01.26), 90 msecs / file, duration = 3954 msecs, parallelization 100%
pngupp.cpp
pngreg.icpp
----- ThrFractRend ( GUI MT MAIN GCC FORCE_SPEED BLITZ WIN32 ) (9 / 9)
BLITZ: ThrFractRend_design.cpp ThrFractRend_sse2.cpp ThrFractRend.cpp main.cpp Paintbox.cpp
plugin\png: 3 file(s) built in (0:04.26), 1420 msecs / file, duration = 8781 msecs, parallelization 100%
Core: 36 file(s) built in (0:11.13), 309 msecs / file, duration = 30922 msecs, parallelization 100%
In file included from C:\MyApps\ThrFractRend\ThrFractRend_design.cpp:42,
from C:/upp/out/ThrFractRend/MINGW.Blitz.Force_speed.Gui.Main.Mt\ $blitz.cpp:3:
C:\MyApps\ThrFractRend\/ThrFractRend.h:84: error: `Mutex' does not name a type
C:\MyApps\ThrFractRend\/ThrFractRend.h:85: error: `Mutex' does not name a type
In file included from C:/upp/out/ThrFractRend/MINGW.Blitz.Force_speed.Gui.Main.Mt\ $blitz.cpp:10:
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp: In member function `void mtfr::Threads::FnMandelbrot(const mtfr::ComplexD&, unsigned int*) const':
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:51: error: `__m128d' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:51: error: (Each undeclared identifier is reported only once for each function it appears in.)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:51: error: expected `;' before "C0"
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:54: error: `C4' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:54: error: `_mm_set1_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:55: error: `C0' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:55: error: `_mm_setr_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:57: error: `X0' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:60: error: `X1' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:60: error: `_mm_mul_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:61: error: `X2' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:61: error: `_mm_unpackhi_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:62: error: `X3' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:62: error: `_mm_add_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:63: error: `_mm_comigt_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:66: error: `X4' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:69: error: `_mm_sub_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:71: error: `_mm_mul_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:75: error: `_mm_unpacklo_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:76: error: `_mm_add_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp: In member function `void mtfr::Threads::FnJulia(const mtfr::ComplexD&, unsigned int*) const':
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:85: error: `__m128d' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:85: error: expected `;' before "C0"
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:88: error: `C4' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:88: error: `_mm_set1_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:89: error: `C0' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:89: error: `_mm_setr_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:90: error: `X0' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:94: error: `X1' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:94: error: `_mm_mul_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:95: error: `X2' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:95: error: `_mm_unpackhi_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:96: error: `X3' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:96: error: `_mm_add_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:97: error: `_mm_comigt_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:100: error: `X4' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:103: error: `_mm_sub_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:105: error: `_mm_mul_sd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:109: error: `_mm_unpacklo_pd' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend_sse2.cpp:110: error: `_mm_add_pd' undeclared (first use this function)
In file included from C:/upp/out/ThrFractRend/MINGW.Blitz.Force_speed.Gui.Main.Mt\ $blitz.cpp:14:
C:\MyApps\ThrFractRend\ThrFractRend.cpp: In member function `void mtfr::Threads::picbox_Paint(Upp::Draw&)':
C:\MyApps\ThrFractRend\ThrFractRend.cpp:82: error: `job_lock' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend.cpp:93: error: `image_lock' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend.cpp: In member function `void mtfr::Threads::ThreadRenderJob(int, int, const Upp::Callback2<const mtfr::ComplexD&, unsigned int*>&, int)':
C:\MyApps\ThrFractRend\ThrFractRend.cpp:131: error: `job_lock' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend.cpp:133: error: `image_lock' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend.cpp:135: error: no matching function for call to `Upp::Semaphore::Release()'
C:/upp/uppsrc/Core/Thread.h:70: note: candidates are: void Upp::Semaphore::Release(int)
C:\MyApps\ThrFractRend\ThrFractRend.cpp: In member function `void mtfr::Threads::Process(const Upp::Callback2<const mtfr::ComplexD&, unsigned int*>&)':
C:\MyApps\ThrFractRend\ThrFractRend.cpp:197: error: `callback4' undeclared (first use this function)
C:\MyApps\ThrFractRend\ThrFractRend.cpp:199: error: `job_lock' undeclared (first use this function)
ThrFractRend: 5 file(s) built in (0:02.48), 496 msecs / file, duration = 4031 msecs, parallelization 16%

There were errors. (1:15.50)



nick

[Updated on: Tue, 17 July 2007 03:42]

Report message to a moderator

Re: Threading example for U++ [message #10575 is a reply to message #10574] Tue, 17 July 2007 11:44 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

1. 2007.1 is to old i tested it with current u++ in SVN on sourceforge
2. Mingw dosn't have SSE2 intrinsics , (gcc & msvc have) comment out USE_SSE2 in ThreadFractRend.h

[Updated on: Tue, 17 July 2007 12:02]

Report message to a moderator

Re: Threading example for U++ [message #10579 is a reply to message #10575] Tue, 17 July 2007 14:52 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12576
Registered: November 2005
Ultimate Member
arturbac wrote on Tue, 17 July 2007 05:44

1. 2007.1 is to old i tested it with current u++ in SVN on sourceforge
2. Mingw dosn't have SSE2 intrinsics , (gcc & msvc have) comment out USE_SSE2 in ThreadFractRend.h




You can add #if defined(COMPILER_GCC) && defined(PLATFORM_WIN32) too..
Re: Threading example for U++ [message #10598 is a reply to message #10199] Wed, 18 July 2007 00:28 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member


Latest Code can be downloaded from this page

http://www.ebasoft.com.pl/index.php?option=com_content&t ask=view&id=21&Itemid=2
Re: Threading example for U++ [message #10964 is a reply to message #10199] Wed, 08 August 2007 02:59 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
this is useful, though it does not show everything i need about thread, like terminating one and communiations b/t threads.
thans anyway
Re: Threading example for U++ [message #10977 is a reply to message #10964] Wed, 08 August 2007 23:38 Go to previous messageGo to next message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

1.Terminating - wrong idea (becouse of allocated resources by threads)
Telling thread to stop via some variable - is good idea
2. Comunicating betwen threads for example with ThreadSafeQueue
when one thread do somting for other thread

Will think about changing the example in the future for use with ThreadSafeQueue...



Re: Threading example for U++ [message #10979 is a reply to message #10977] Thu, 09 August 2007 05:35 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
arturbac wrote on Thu, 09 August 2007 05:38

1.Terminating - wrong idea (becouse of allocated resources by threads)
Telling thread to stop via some variable - is good idea
2. Comunicating betwen threads for example with ThreadSafeQueue
when one thread do somting for other thread

Will think about changing the example in the future for use with ThreadSafeQueue...



where is ThreadSafeQueue? not in U++ or your example?
it seems i can use select(). but i still don't know how to put this kind of queue or sth. together with a SOCKET into fd_set
Re: Threading example for U++ [message #10985 is a reply to message #10979] Thu, 09 August 2007 21:59 Go to previous messageGo to previous message
arturbac is currently offline  arturbac
Messages: 91
Registered: May 2007
Location: Reda, Poland
Member

I will put here my fresh one hoever this [C++] implementation is not tested, but Same Generic in C# works well.
[Im at porting stage of some app to c++ so why it is stil not tested]

	template <typename T>
	class ThreadSafeQueue
	{
		class QueueElement;
	public :
			
		int							GetCount() const;
		void						Enqueue(T value);
		T							Dequeue();
		void						FinishWaiting(bool end_job);
		
		ThreadSafeQueue();
		~ThreadSafeQueue();

	private:
		int							m_count;
		QueueElement *				m_head;
		QueueElement *				m_tail;
		Threading::Monitor			m_monitor;
		Threading::CriticalSection	m_lock;
		bool						m_finish_wait;


		class QueueElement
		{
		private:
			friend class				ThreadSafeQueue;

			T							m_value;
			QueueElement *				m_next;

			T							GetValue() const;

			QueueElement(const T & value);
		};
	};



	template <typename T> 
	int ThreadSafeQueue<T>::GetCount() const 
	{ 
		return this->m_count; 
	}

	template <typename T> 
	ThreadSafeQueue<T>::ThreadSafeQueue()
		: m_head(NULL)
		, m_finish_wait(false)
		, m_tail(NULL)
		, m_count(0) 
	{}
	template <typename T> 
	ThreadSafeQueue<T>::~ThreadSafeQueue()
	{
		m_lock.Enter();
		//Clenup remaining items
		if (this->m_head != NULL)
		{ 
			QueueElement *p = this->m_head, *pdel;
			while (p != NULL)
			{
				pdel = p;
				p = p->m_next;
				delete pdel;
			}
			this->m_head = NULL;
			this->m_tail = NULL;
		}
		m_lock.Leave();
	}
	template <typename T> 
	void ThreadSafeQueue<T>::Enqueue(T value)
	{
		m_lock.Enter();
		{
			m_count++;
			QueueElement *enqueue = new QueueElement(value);
			if (m_head == NULL)
			{
				m_head = enqueue;
				m_monitor.Pulse();
			}
			else
				m_tail->m_next = enqueue;
			m_tail = enqueue;
		}
		m_lock.Leave();
	}
	template <typename T> 
	T ThreadSafeQueue<T>::Dequeue()
	{
		QueueElement *dequeued;
		m_lock.Enter();
		{
			while (m_head == NULL && !m_finish_wait)
				m_monitor.Wait();
			if (m_head == NULL && m_finish_wait)
			{
				m_lock.Leave();
				return T(); //End
			}
			m_count--;
			dequeued = m_head;
			m_head = m_head->m_next;
		}
		m_lock.Leave();
		T res(dequeued->GetValue());
		delete dequeued;
		return res;
	}
	template <typename T> 
	void ThreadSafeQueue<T>::FinishWaiting(bool end_job)
	{
		m_lock.Enter();
		{
			m_finish_wait = end_job;
			m_monitor.Pulse();
		}
		m_lock.Leave();
	}

	template <typename T> 
	ThreadSafeQueue<T>::QueueElement::QueueElement(const T & value)
		: m_next(NULL)
		, m_value(value)
	{}

	template <typename T> 
	T ThreadSafeQueue<T>::QueueElement::GetValue() const 
	{ 
		return this->m_value; 
	}


	class Monitor : public CriticalSection
	{
	public:

		Monitor();

		void						Wait() throw(...);
		void						Pulse() throw(...);

	private:
		int							m_QueueSize;
		CriticalSection				m_lock;
		Semaphore					m_waiting;
	};

	Monitor::Monitor() : m_QueueSize(0) {}
	void Monitor::Wait()
	{
		m_lock.Enter();
		m_QueueSize++;
		m_lock.Leave();
		m_waiting.Wait();
	}  
	void Monitor::Pulse()
	{
		m_lock.Enter();
		if (m_QueueSize > 0)
			m_waiting.Release();
		m_lock.Leave();
	}
Previous Topic: Non-Blocking socket example
Next Topic: typo in manual on web
Goto Forum:
  


Current Time: Tue Jul 14 09:08:24 CEST 2020

Total time taken to generate the page: 0.01898 seconds