How are theads, Tid and spawn related?

Russel Winder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 2 04:31:30 PST 2014


On Sat, 2014-11-01 at 23:32 +0000, Neven via Digitalmars-d-learn wrote:
> Ok, a newbie question ahead. I want to create new thread which 
> calls given function with some parameters. Thus, I think spawn is 
> the right function for me. However that functions returns Tid and 
> not a Thread object.
> 
> So I want to know how can I make a Thread object out of it. What 
> I would like to achieve is to wait for spawned thread to finish 
> its execution, that is join with main thread.

Modern thinking is that threads are managed infrastructure that
application code should never manage explicitly. In this way of thinking
application code only cares about data readiness and/or message passing.

With this mind set, using D, you spawn a task and get a Tid in response.
The Tid allows you to send messages to the asynchronous activity and to
receive messages from it. This is all very, very close to actors.

Underneath the actors, there are threads, but your code doesn't care,
they are managed resource. All you code cares about is sending messages
between asynchronous tasks.

As an example of spawning a function and waiting for it to complete
using spawn you might look at: 

https://github.com/russel/Pi_Quadrature/blob/master/D/pi_spawn.d

-- 
Russel.
=============================================================================
Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder at ekiga.net
41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141102/588adc92/attachment.sig>


More information about the Digitalmars-d-learn mailing list