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 » Thread based "conveyor" class for Win32
Thread based "conveyor" class for Win32 [message #12083] Wed, 10 October 2007 18:51 Go to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

In my work I have rather usual task of having some queue service, processing main thread requests in background. I just finished general purpose class template for simplified handling of such a task. It`s rather young (with interface unpolished) but look at what you can do with it:
template<class T> class ConveyorThread : protected Thread
{ typedef ConveyorThread CLASSNAME;
public:
	ConveyorThread(bool enabled = true);
	virtual ~ConveyorThread();
	void Enable(bool enable = true);

	void Request(Callback1<const T &>handler, const T &request);
	void ClearAllRequests();

	void SharedEnter();
	void SharedLeave();

	void RequestFinish();
	int  WaitFinished();
	bool IsFinished();
}


Just add request for asyncronous processing and define handler for it - and that`s all. Rather convenient, I suppose.
Class also has guarding functions for interacting with main thread`s objects: SharedEnter/SharedLeave (for updating interface, statistics, etc).

Any suggestions, critics and recommendations is appreciated.
Re: Thread based "conveyor" class for Win32 [message #12086 is a reply to message #12083] Wed, 10 October 2007 19:35 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Wed, 10 October 2007 12:51

In my work I have rather usual task of having some queue service, processing main thread requests in background. I just finished general purpose class template for simplified handling of such a task. It`s rather young (with interface unpolished) but look at what you can do with it:
template<class T> class ConveyorThread : protected Thread
{ typedef ConveyorThread CLASSNAME;
public:
	ConveyorThread(bool enabled = true);
	virtual ~ConveyorThread();
	void Enable(bool enable = true);

	void Request(Callback1<const T &>handler, const T &request);
	void ClearAllRequests();

	void SharedEnter();
	void SharedLeave();

	void RequestFinish();
	int  WaitFinished();
	bool IsFinished();
}


Just add request for asyncronous processing and define handler for it - and that`s all. Rather convenient, I suppose.
Class also has guarding functions for interacting with main thread`s objects: SharedEnter/SharedLeave (for updating interface, statistics, etc).

Any suggestions, critics and recommendations is appreciated.


Interesting idea. Is there anything that prevents using Semaphore instead of events there? (The problem is that your code is win32 specific).
Re: Thread based "conveyor" class for Win32 [message #12087 is a reply to message #12086] Wed, 10 October 2007 19:44 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

No Message Body

[Updated on: Wed, 10 October 2007 20:21]

Report message to a moderator

Re: Thread based "conveyor" class for Win32 [message #12088 is a reply to message #12087] Wed, 10 October 2007 19:50 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

I just recollected the cause of using Windows objects instead of Semaphore class: the need of WaitForMultipleObjects call. Also, I was needed for testing of object state with WaitForSingleObject(finishEvent, 0)

[Updated on: Wed, 10 October 2007 20:00]

Report message to a moderator

Re: Thread based "conveyor" class for Win32 [message #12093 is a reply to message #12088] Wed, 10 October 2007 20:52 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Wed, 10 October 2007 13:50

I just recollected the cause of using Windows objects instead of Semaphore class: the need of WaitForMultipleObjects call. Also, I was needed for testing of object state with WaitForSingleObject(finishEvent, 0)


Thanks.

Mirek
Re: Thread based "conveyor" class for Win32 [message #13115 is a reply to message #12093] Wed, 12 December 2007 23:16 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Recently I finished project. It included new upgraded version of ConveyorThread and brand new RS232Thread class.
RS232Thread is really advanced helper class useful i.e. for industrial automation, when it`s necessary to have some good tool for programming COM-port (RS232) i/o.
I`d like to contribute these classes into some package into U++. For now, they are targeted for Win32 only. It is due to Mutex and Semaphore limitations listed above.
I`m no professional in Linux, but if these base sync classes functionality extended, I`ll try to write complete multiplatform ConveyorThread and RS232Thread.
Is it possible to add necessary functionality to these classes?
Re: Thread based "conveyor" class for Win32 [message #13152 is a reply to message #13115] Sun, 16 December 2007 22:27 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Wed, 12 December 2007 17:16

Recently I finished project. It included new upgraded version of ConveyorThread and brand new RS232Thread class.
RS232Thread is really advanced helper class useful i.e. for industrial automation, when it`s necessary to have some good tool for programming COM-port (RS232) i/o.
I`d like to contribute these classes into some package into U++. For now, they are targeted for Win32 only. It is due to Mutex and Semaphore limitations listed above.
I`m no professional in Linux, but if these base sync classes functionality extended, I`ll try to write complete multiplatform ConveyorThread and RS232Thread.
Is it possible to add necessary functionality to these classes?


Maybe it will be necessarry to just #ifdef PLATFORM_LINUX and use Linux API here...

Mirek
Re: Thread based "conveyor" class for Win32 [message #13158 is a reply to message #13115] Mon, 17 December 2007 00:03 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
Mindtraveller wrote on Wed, 12 December 2007 23:16

Recently I finished project. It included new upgraded version of ConveyorThread and brand new RS232Thread class.
RS232Thread is really advanced helper class useful i.e. for industrial automation, when it`s necessary to have some good tool for programming COM-port (RS232) i/o.
I`d like to contribute these classes into some package into U++. For now, they are targeted for Win32 only. It is due to Mutex and Semaphore limitations listed above.



I think, you can post it here and a good Linux programmer could finish it. These classes seems to be very interesting Smile
Re: Thread based "conveyor" class for Win32 [message #13162 is a reply to message #13158] Mon, 17 December 2007 12:50 Go to previous message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Recently I`ve been thinking about implementing Mutex and Semaphore with additional functionality and how to do it the best way.

And that`s my suggestion: it is possible to make these classes Multithreading POSIX API based. It will work OK for Linux version. And for Win32 the same functions will be mapped by POSIX Threads for Win32 library calls. Here`s library description: <a href="http://sourceware.org/pthreads-win32/conformance.html"></a>

This will unify, ease debugging and reading of code.

[Updated on: Mon, 17 December 2007 12:52]

Report message to a moderator

Previous Topic: will implement a UDP class?
Next Topic: SocketEvent
Goto Forum:
  


Current Time: Fri Mar 29 08:38:13 CET 2024

Total time taken to generate the page: 0.01424 seconds