Help with concurrent threads

[email protected] (alfonso caponi) Fri, 16 Apr 2010 17:48:41 +0200
Newsgroups perl.ithreads
Message-ID <[email protected]>
--0016e6d7e4b2a58c8b04845c8b07
Content-Type: multipart/alternative; boundary=0016e6d7e4b2a58c8304845c8b05

--0016e6d7e4b2a58c8304845c8b05
Content-Type: text/plain; charset=ISO-8859-1

Hi list,

I've some doubts with a multi-threads script. My goal is to create a script
with the same max number of threads running at all times.

For example: I've an array with 9 elements and 3 threads to process it; each
thread can process an element using a different time (a thread could end
before another) and I would like 3 threads always running.

My idea is to take an element from a queue, start threads to reach the
maximum number of active threads allowed and simultaneously start a separate
(but unique) thread to wait every thread in execution.

In attachment my "solution". Have you any tips? I would not use other
modules as well threads, threads::shared, thread::queue.

Thank you very much!
Al

--0016e6d7e4b2a58c8304845c8b05
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi list,<br><br><span id=3D"result_box" class=3D"short_text"><span style=3D=
"background-color: rgb(255, 255, 255);" title=3D"ho qualche dubbio con un s=
emplice script.">I&#39;ve some doubts with a multi-threads script.</span></=
span> <span id=3D"result_box" class=3D"short_text"><span style=3D"backgroun=
d-color: rgb(255, 255, 255);" title=3D"Il mio obbiettivo =E8 di creare uno =
script che
 abbia un certo numero di thread sempre in esecuzione.">My goal is to creat=
e a script with the same max number of threads running at all times.</span>=
</span><br><br><span id=3D"result_box" class=3D"short_text"><span style=3D"=
background-color: rgb(255, 255, 255);" title=3D"Per esempio">For example:</=
span></span> I&#39;ve an array with 9 elements and 3 threads to process it;=
 <span id=3D"result_box" class=3D"long_text"><span style=3D"background-colo=
r: rgb(255, 255, 255);" title=3D"ogni thread pu=F2 elaborare un oggetto=20
utilizzando un tempo differente (un thread potrebbe finire prima di un=20
altro) e vorrei che non ci fosse un tempo di attesa per aspettare che=20
ogni thread sia terminato.">each thread can process an element using a diff=
erent time (a thread could end before another) and I would like 3 threads a=
lways running.</span><span style=3D"background-color: rgb(255, 255, 255);" =
title=3D"Quindi avere sempre
 3 thread in lavorazione effettiva."><br><br></span></span><span id=3D"resu=
lt_box" class=3D"long_text"><span style=3D"background-color: rgb(255, 255, =
255);" title=3D"La mia idea =E8 di prelevare un oggetto da una
 coda, avviare un thread fino al raggiungere del numero massimo di=20
thread attivi permessi (nell&#39;esempio 3) e contemporaneamente avviare un=
=20
thread separato che faccia il join per i thread che man mano finiscono.">My
 idea is to take an element from a queue, start threads to reach the=20
maximum number of active threads allowed and simultaneously=20
start a separate (but unique) thread to wait every thread in execution.</sp=
an></span><br>
<br>In attachment my &quot;solution&quot;.<span id=3D"result_box" class=3D"=
short_text"><span style=3D"background-color: rgb(255, 255, 255);" title=3D"=
Avete consigli, pareri, aiuto per=20
migliorare?"> Have you any tips? </span></span><span id=3D"result_box" clas=
s=3D"short_text"><span style=3D"background-color: rgb(255, 255, 255);" titl=
e=3D"Io non vorrei usare altri moduli oltre a">I=20
would not use other modules as well</span></span> threads, threads::shared,=
 thread::queue.<br><br>Thank you very much!<br>Al

--0016e6d7e4b2a58c8304845c8b05--
--0016e6d7e4b2a58c8b04845c8b07
Content-Type: text/plain; charset=US-ASCII; name="test_thread.txt"
Content-Disposition: attachment; filename="test_thread.txt"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_g835kkp10

IyEvdXNyL2Jpbi9wZXJsIC13DQoNCiR8PTE7DQoNCnVzZSBzdHJpY3Q7DQp1c2UgdGhyZWFkczsN
CnVzZSB0aHJlYWRzOjpzaGFyZWQ7DQp1c2UgVGhyZWFkOjpRdWV1ZTsNCg0KbXkgJG1heHRocmVh
ZHMgPSAzOw0KDQpteSBAdXNlcnMgPSAoJ2ppbScsJ2phbmUnLCd0b20nLCd0aW0nLCdhbCcsJ2Vk
JywnbWFyeScsJ2tpbScsJ21lZycpOw0KDQpvdXIgJHEgOnNoYXJlZDsNCg0KJHEgPSBuZXcgVGhy
ZWFkOjpRdWV1ZTsNCg0KIyBmaWxsIHRoZSBxdWV1ZQ0KJHEtPmVucXVldWUoJ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