Can/should spawn work with functions that return?

Jonathan M Davis jmdavisProg at gmx.com
Fri Jan 21 18:00:38 PST 2011


On Friday, January 21, 2011 15:23:37 Andrej Mitrovic wrote:
> Sorry, I should be careful with the word side-effects. What I meant
> was the newly spawned thread does its own job that the main thread
> doesn't care much about. It doesn't touch main's state or any shared
> variables. It does some work other than return a value. But the
> function I wanted the new thread to execute happened to have a return
> value, so I couldn't spawn the thread.
> 
> Really, the issue is that sometimes I have to convert C code to D and
> concurrency is dealt with differently in the two languages, not to
> mention the fact that I have next to no experience with using threads
> in any language (but I'm forced to use them in some cases).
> 
> So, sometimes I'll ask silly questions because I'm very new to this,
> but hopefully I'll learn a thing or two. I do enjoy your long and
> informative responses though, Jonathan. :)

Well, I'd suggest just wrapping the function call in a lambda, unless spawn 
doesn't like that for some reason. I suppose that spawn could be made take 
functions with return values, but it's not generally something that you'd do, 
and it _is_ pretty easy to wrap such functions, so it seems to me like it's 
probably better as it is - though I can see why it would be annoying if you kept 
needing to spawn functions with return values. Still, that makes me wonder about 
what you're doing. I guess that you could be doing I/O or somethnig, but you 
certainly can't be talking back to the parent thread without having written 
things that way (in which case, you'd have made the spawned function void). You 
could always put in an enhancement request, though it does seem wrong to me 
(conceptually-speaking, if nothing else) to have a spawned function with a 
return value.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list