Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2015-10

Simplest way to have a safe thread queue

8 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Aradeonas

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[1] and queue [2]wiki and some posts like this [3]and also Lazarus multi threading samples but still cant find out how to make a good structure that really work specially when I read [4]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

Links:

1. http://wiki.freepascal.org/Multithreaded_Application_Tutorial
2. http://wiki.freepascal.org/Manager_Worker_Threads_System
3. http://stackoverflow.com/questions/15027726/how-to-make-a-thread-finish-its-work-before-being-freed/15033839#15033839
4. http://stackoverflow.com/questions/15622261/multi-thread-delphi

http://www.fastmail.com - Access all of your messages and folders wherever you are -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151021/68bbe3bb/attachment.html>

Reply by email

Michael Schnell
to Aradeonas: Hi, [Cross posted on forum so if you want answer there :…
Aradeonas wrote
I want to make a simple and safe queue

What exactly do you mean by "Thread Queue" ?

Is this the same as a "Thread Pool" (i.e. severakl threads that wait for work and can be assigned a task if necessary without the need of creating a new thread)

I once did a thread pool implementation. But I think that there are several more implementations that are better tested than mine.

-Michael

Reply by email

Tony Whyman
to Aradeonas: Hi, [Cross posted on forum so if you want answer there :…

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

Aradeonas wrote 50 lines
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 http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151021/b1b42968/attachment-0001.html>

Reply by email

Aradeonas
to Aradeonas

I like to have o powerfull solution but an easy way like this[1] also will do my job for now.

Regards, Ara

Links:

1. http://stackoverflow.com/questions/15027726/how-to-make-a-thread-finish-its-work-before-being-freed/15033839#15033839

http://www.fastmail.com - A no graphics, no pop-ups email service -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151021/d06f0b6e/attachment.html>

Reply by email

Tony Whyman
to Aradeonas: Thanks?Tony,Can you help me more where should I look? Regards, Ara…

Look in xv5ipc.inc for the Linux implementation and winipc.inc for the Windows implementation. You are probably not interested in the class TGlobalInterface (other than as example code) as this is the IBSQLMonitor use of the IPC. The other classes may be of interested and are documented in the code.

Aradeonas wrote 12 lines
Thanks Tony,Can you help me more where should I look? Regards, Ara -- http://www.fastmail.com - A no graphics, no pop-ups email service -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151021/01352d8a/attachment.html>

Reply by email