[Lazarus] Simplest way to have a safe thread queue
Tony Whyman
tony.whyman at mccallumwhyman.com
Wed Oct 21 12:28:43 CEST 2015
Aradeonas,
You will probably need to start using semaphores/mutexes to control
access from multiple threads if your requirement goes beyond the simple
use of critical sections. The problem starts becoming more complex if
one or more threads has to wait on the actions of another before
accessing the queue. A further complexity is that you are entering the
world of platform dependency with significant differences between
Windows and Unixes in semaphore and mutex semantics.
This is a problem I had to solve when porting the IBX TISQLMonitor class
to Lazarus. You are welcome to have a look at the solution I came up
with which hides the platform differences behind a common set of classes
that are implemented differently for each platform. These implement the
common set of objects needed to co-ordinate multiple threads accessing
common queues in the same or different processes: shared memory,
mutexes, single and multi-lock gates.
You can download the source at http://www.mwasoftware.co.uk/ibx
Regards
Tony Whyman
MWA
On 21/10/15 11:07, Aradeonas wrote:
> Hi,
> [Cross posted on forum so if you want answer there :
> http://forum.lazarus.freepascal.org/index.php/topic,30097.0.html]
> I want to make a simple and safe queue with multiple worker that do
> the jobs, so for example I add 10 job and have 2 worker that do the job.
> For this I read multi threading
> <http://wiki.freepascal.org/Multithreaded_Application_Tutorial> and
> queue <http://wiki.freepascal.org/Manager_Worker_Threads_System>wiki
> and some posts like this
> <http://stackoverflow.com/questions/15027726/how-to-make-a-thread-finish-its-work-before-being-freed/15033839#15033839>and
> also Lazarus multi threading samples but still cant find out how to
> make a good structure that really work specially when I read
> <http://stackoverflow.com/questions/15622261/multi-thread-delphi>about
> that it is not very good to do counting and micro-managing.
> Also I dont know a good multi threading library in Lazarus like
> OmniThreadLibrary and also couldn't find any good example.
> For now I have a list of jobs and 2 workers and also a var that
> control that when a thread finish start another for the next job but
> it not do job very good and sometimes it will mess the list of jobs.
> var
> i: integer;
> begin
> try
> MyCriticalSection.Enter;
> for i := 0 to Count - 1 do
> begin
> if (StartedCount >= WorkersCount) then //check for is there need
> for making new thread or not
> Exit;
> if (not Jobs[i].Working) and (not Jobs[i].Finished) then //check
> the job is already done or not
> StartJob(Jobs[i]);
> end;
> if (StartedCount = 0) then
> Finish;//It will finish that list of jobs
> finally
> MyCriticalSection.Leave;
> end;
> Regards,
> Ara
> --
> http://www.fastmail.com - Access all of your messages and folders
> wherever you are
>
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20151021/b1b42968/attachment-0003.html>
More information about the Lazarus
mailing list