<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Aradeonas,<br>
<br>
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.<br>
<br>
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.<br>
<br>
You can download the source at <a class="moz-txt-link-freetext" href="http://www.mwasoftware.co.uk/ibx">http://www.mwasoftware.co.uk/ibx</a><br>
<br>
Regards<br>
<br>
Tony Whyman<br>
MWA<br>
<br>
<br>
<div class="moz-cite-prefix">On 21/10/15 11:07, Aradeonas wrote:<br>
</div>
<blockquote
cite="mid:1445422068.1894605.416162289.643AF7B3@webmail.messagingengine.com"
type="cite">
<title></title>
<div>Hi,<br>
</div>
<pre class="defanged12-defanged3-u-article">[Cross posted on forum so if you want answer there :
<a moz-do-not-send="true" href="http://forum.lazarus.freepascal.org/index.php/topic,30097.0.html">http://forum.lazarus.freepascal.org/index.php/topic,30097.0.html</a>]
</pre>
<div>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.<br>
</div>
<div>For this I read <a moz-do-not-send="true"
href="http://wiki.freepascal.org/Multithreaded_Application_Tutorial">multi
threading</a> and <a moz-do-not-send="true"
href="http://wiki.freepascal.org/Manager_Worker_Threads_System">queue
</a>wiki and some posts like <a moz-do-not-send="true"
href="http://stackoverflow.com/questions/15027726/how-to-make-a-thread-finish-its-work-before-being-freed/15033839#15033839">this
</a>and also Lazarus multi threading samples but still cant find
out how to make a good structure that really work specially when
I <a moz-do-not-send="true"
href="http://stackoverflow.com/questions/15622261/multi-thread-delphi">read
</a>about that it is not very good to do counting and
micro-managing.<br>
</div>
<div>Also I dont know a good multi threading library in Lazarus
like OmniThreadLibrary and also couldn't find any good example.<br>
</div>
<div> </div>
<div>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.<br>
</div>
<div> </div>
<div>var<br>
</div>
<div> i: integer;<br>
</div>
<div>begin<br>
</div>
<div> try<br>
</div>
<div> MyCriticalSection.Enter;<br>
</div>
<div> for i := 0 to Count - 1 do<br>
</div>
<div> begin<br>
</div>
<div> if (StartedCount >= WorkersCount) then //check for
is there need for making new thread or not<br>
</div>
<div> Exit;<br>
</div>
<div> if (not Jobs[i].Working) and (not Jobs[i].Finished)
then //check the job is already done or not<br>
</div>
<div> StartJob(Jobs[i]);<br>
</div>
<div> end;<br>
</div>
<div> if (StartedCount = 0) then<br>
</div>
<div> Finish;//It will finish that list of jobs<br>
</div>
<div> finally<br>
</div>
<div> MyCriticalSection.Leave;<br>
</div>
<div> end; <br>
</div>
<div> </div>
<div>Regards,<br>
</div>
<div>Ara<br>
</div>
<pre>--
<a class="moz-txt-link-freetext" href="http://www.fastmail.com">http://www.fastmail.com</a> - Access all of your messages and folders
wherever you are
</pre>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">--
_______________________________________________
Lazarus mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Lazarus@lists.lazarus.freepascal.org">Lazarus@lists.lazarus.freepascal.org</a>
<a class="moz-txt-link-freetext" href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus">http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus</a>
</pre>
</blockquote>
<br>
</body>
</html>