| View previous topic :: View next topic |
| Author |
Message |
Scott Martin Guest
|
Posted: Thu Jun 29, 2006 5:58 am Post subject: Solid Thread Code |
|
|
I was looking for a set of Routines for creating/handling threads.
(I am not fluent in using threads)
In researching the NG's, I understand there are caveats in using the general code
provided with the Delphi examples?? and I was wondering if there was something
else available?
I have an instance where I need to create threads in a app and would like some
direction on something that is solid and tested.
Regards,
Scott. |
|
| Back to top |
|
 |
John Gray Guest
|
Posted: Thu Jun 29, 2006 7:09 am Post subject: Re: Solid Thread Code |
|
|
Boian Mitov has a free component called "BMThread",
which might help: http://mitov.com
hth,
John
----------------------------
"Scott Martin" wrote...
| Quote: | I was looking for a set of Routines for creating/handling threads.
(I am not fluent in using threads)
[...] |
|
|
| Back to top |
|
 |
Stephen Quinn Guest
|
|
| Back to top |
|
 |
Thorsten Engler [NexusDB] Guest
|
Posted: Thu Jun 29, 2006 7:36 am Post subject: Re: Solid Thread Code |
|
|
Scott Martin wrote:
| Quote: | I was looking for a set of Routines for creating/handling threads.
(I am not fluent in using threads)
In researching the NG's, I understand there are caveats in using the general code
provided with the Delphi examples?? and I was wondering if there was something
else available?
I have an instance where I need to create threads in a app and would like some
direction on something that is solid and tested.
I don't know what your exact requirements are. But NexusDB includes a |
unit nxllThread. The classes in there are used everywhere in the NexusDB
server and it's running solid even with 100's of threads. The classes in
particular:
TnxThread:
- integrated support for showing threadnames in the delphi debugger
- WaitForEx(aTimeout : Integer);
- you write your code in DoExecute instead of Execute. There is a
DoHandleException(E: Exception) method you can implement to properly
handle any exceptions that the thread would normally throw out of it's
execute function.
- constructor allows specifying a thread priority
TnxInitThread = class(TnxThread):
- adds an DoBeforeExecute, DoAfterExecute, and InnerExecute methods.
- You write your main code InnerExecute
- DoBeforeExecute is executed *in the context of the new thread*
- Until DoBeforeExecute is finished the thread that created this new
thread is blocked inside the TnxInitThread.Create call
- if an exception is raised in DoBeforeExecute that exception is
correctly transfered and reraised in the context of the thread that
called TnxInitThread.Create
- if DoBeforeExecute failed with an exception neither InnerExecute nor
DoAfterExecute will be called.
- if DoBeforeExecute did not fail then DoAfterExecute is guaranteed to
be called.
TnxWaitThread = class(TnxInitThread):
- thread will wait inside DoAfterExecute
- property Waiting: Boolean signals if the thread is waiting
- procedure AllowExit or destroying the thread will stop the waiting
- when overwriting DoAfterExecute you can have your code executed before
or after the wait depending if you write your code before or after
calling inherited.
TnxMessageThread = class(TnxInitThread):
- implements a message loop in InnerExecute. You usually do NOT
overwrite InnerExecute.
- creates a window handle in the context of that thread (property
Handle: THandle)
- messages send to that window handle are dispatched to the
TnxMessageThread object in the same way as messages are dispatched to
TWinControl. Meaning you can implement methods like:
procedure WMSysColorChange(var Message: TWMSysColorChange); message
WM_SYSCOLORCHANGE;
and messages send to that window handle will end up calling the
appropriate message method.
- has PerformSend and PerformPost methods for easily sending messages to
that handle.
- to stop the message loop either call PostQuitMessage or call
WaitForQuit (implicitly calls PostQuitMessage) or just destroy the
thread object (implicitly calls WaitForQuit)
TnxTimerThread = class(TnxInitThread):
- has a DoTimer method that you need to override
- constructor takes an aInterval : Integer;
- there is an aInterval pause between the time that DoTimer returned and
the time it's called again.
There are also some additional utility functions in that unit that have
to do with threads. In addition there is code for dynamically binding to
TimerQueue, thread pool and IOCP API functions supported by newer
versions of windows.
I know you have NexusDB C/S so you already got that unit.
For everyone else interested:
That unit is included (DCU only) with our free embedded version of
NexusDB2. And the cheapeast way to get the source would be a license of
the Nexus Memory Manager.
Both to be found at: http://www.nexusdb.com
Cheers,
Thorsten |
|
| Back to top |
|
 |
Scott Martin Guest
|
Posted: Thu Jun 29, 2006 8:02 am Post subject: Re: Solid Thread Code |
|
|
Thorsten,
Thank you very much! Nice to know I already have the code!
(love it when that happens ;)
Regards,
Scott.
"Thorsten Engler [NexusDB]" <thorsten.englerNO (AT) SPAMnexusdb (DOT) com> wrote in message
news:44a33b1d$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Scott Martin wrote:
I was looking for a set of Routines for creating/handling threads.
(I am not fluent in using threads)
In researching the NG's, I understand there are caveats in using the general code
provided with the Delphi examples?? and I was wondering if there was something
else available?
I have an instance where I need to create threads in a app and would like some
direction on something that is solid and tested.
I don't know what your exact requirements are. But NexusDB includes a
unit nxllThread. The classes in there are used everywhere in the NexusDB
server and it's running solid even with 100's of threads. The classes in
particular:
TnxThread:
- integrated support for showing threadnames in the delphi debugger
- WaitForEx(aTimeout : Integer);
- you write your code in DoExecute instead of Execute. There is a
DoHandleException(E: Exception) method you can implement to properly
handle any exceptions that the thread would normally throw out of it's
execute function.
- constructor allows specifying a thread priority
TnxInitThread = class(TnxThread):
- adds an DoBeforeExecute, DoAfterExecute, and InnerExecute methods.
- You write your main code InnerExecute
- DoBeforeExecute is executed *in the context of the new thread*
- Until DoBeforeExecute is finished the thread that created this new
thread is blocked inside the TnxInitThread.Create call
- if an exception is raised in DoBeforeExecute that exception is
correctly transfered and reraised in the context of the thread that
called TnxInitThread.Create
- if DoBeforeExecute failed with an exception neither InnerExecute nor
DoAfterExecute will be called.
- if DoBeforeExecute did not fail then DoAfterExecute is guaranteed to
be called.
TnxWaitThread = class(TnxInitThread):
- thread will wait inside DoAfterExecute
- property Waiting: Boolean signals if the thread is waiting
- procedure AllowExit or destroying the thread will stop the waiting
- when overwriting DoAfterExecute you can have your code executed before
or after the wait depending if you write your code before or after
calling inherited.
TnxMessageThread = class(TnxInitThread):
- implements a message loop in InnerExecute. You usually do NOT
overwrite InnerExecute.
- creates a window handle in the context of that thread (property
Handle: THandle)
- messages send to that window handle are dispatched to the
TnxMessageThread object in the same way as messages are dispatched to
TWinControl. Meaning you can implement methods like:
procedure WMSysColorChange(var Message: TWMSysColorChange); message
WM_SYSCOLORCHANGE;
and messages send to that window handle will end up calling the
appropriate message method.
- has PerformSend and PerformPost methods for easily sending messages to
that handle.
- to stop the message loop either call PostQuitMessage or call
WaitForQuit (implicitly calls PostQuitMessage) or just destroy the
thread object (implicitly calls WaitForQuit)
TnxTimerThread = class(TnxInitThread):
- has a DoTimer method that you need to override
- constructor takes an aInterval : Integer;
- there is an aInterval pause between the time that DoTimer returned and
the time it's called again.
There are also some additional utility functions in that unit that have
to do with threads. In addition there is code for dynamically binding to
TimerQueue, thread pool and IOCP API functions supported by newer
versions of windows.
I know you have NexusDB C/S so you already got that unit.
For everyone else interested:
That unit is included (DCU only) with our free embedded version of
NexusDB2. And the cheapeast way to get the source would be a license of
the Nexus Memory Manager.
Both to be found at: http://www.nexusdb.com
Cheers,
Thorsten |
|
|
| Back to top |
|
 |
Liz Guest
|
Posted: Thu Jun 29, 2006 8:12 am Post subject: Re: Solid Thread Code |
|
|
Scott Martin wrote:
| Quote: | I have an instance where I need to create threads in a app and would
like some direction on something that is solid and tested.
|
www.profscomponents.com
have a nice set of threaded components which mean you dont have to
worry too much on what does what as its very clear what you should and
shouldnt do in certain areas
such as the .execute says "runs on the thread" so you know no vcl calls
the after execute however says it runs on the vcl thread, so you know
you can do what you like there..
plus it has a nice way of passing parameters between them all
--
Liz the Brit
Delphi things I have released: http://www.xcalibur.co.uk/DelphiThings |
|
| Back to top |
|
 |
Thorsten Engler [NexusDB] Guest
|
Posted: Thu Jun 29, 2006 8:12 am Post subject: Re: Solid Thread Code |
|
|
If you have specific questions about how to use these classes or
multi-threaded development in general, feel free to ask in the NexusDB
newsgroup or contact me via the chat on our website.
NexusDB code also contains TnxEvent, TnxPadlock, TnxReadWritePortal,
TnxThreadSafeStack and TnxEventPool in nxllSync.pas which should proof
very useful for multi-threaded code. In addition, nxllLockedFuncs.pas
contains whole bunch of atomic add/sub/inc/dec/xchg/cmpxchg functions
and low level implementations of spin locks and a lock free thread-safe
stack implementation. nxllThreadSafeQueue.pas contains an efficient
implementation of a thread safe queue under the assumption that enqueue
operations take place rarely and when then as batches. Dequeue take
place lock-free. So you can enqueue batches of work items and have a
pool of threads that dequeue them and process. nxllList.pas implements
an efficient list (TnxList) and sorted list (TnxSortedList). There is a
base class TnxListSyncAccess and derived from that TnxListPortal and
TnxListPadlock which can be used as wrappers around the 2 list classes
to make them threadsafe. Either with separate read/write locks or with a
general criticl section lock.
All that can help with the implementation stable and fast multi-threaded
code. All the code is well tested and stable as it's the foundation of
the NexusDB Core.
Cheers,
Thorsten
Scott Martin wrote:
| Quote: | Thorsten,
Thank you very much! Nice to know I already have the code!
(love it when that happens ;)
Regards,
Scott.
|
--
Thorsten Engler
Nexus Database Systems. Pty. Ltd.
http://www.nexusdb.com |
|
| Back to top |
|
 |
Dennis Passmore Guest
|
Posted: Tue Jul 04, 2006 4:07 am Post subject: Re: Solid Thread Code |
|
|
There is a demo project included in the Delphi download (BMDelphiThread.zip) which was written for
Delphi 5 which works just fine in BDS 2006 and shows how to implement multiple threads.
Dennis Passmore
"If you cannot conceive the idea you
will never achieve the desired results" |
|
| Back to top |
|
 |
|