std.concurrency.spawn does not accept delegates

Simen Kjaeraas simen.kjaras at gmail.com
Mon Jul 18 10:54:24 PDT 2011


On Mon, 18 Jul 2011 18:06:46 +0200, Jonathan M Davis <jmdavisProg at gmx.com>  
wrote:

> On Monday 18 July 2011 15:55:52 teo wrote:
>> On Mon, 18 Jul 2011 10:26:27 -0400, Steven Schveighoffer wrote:
>> > On Sun, 17 Jul 2011 15:29:02 -0400, teo <teo.ubuntu at yahoo.com> wrote:
>> >> It looks like std.concurrency.spawn does not accept delegates. Is
>> >> there
>> >> any reason for that?
>> >
>> > There is no type attached to the hidden 'this' pointer.  So spawn  
>> cannot
>> > guarantee it doesn't point to unshared data.
>> >
>> > -Steve
>>
>> Bad. I tried to pass as an argument a pointer to an instance of a class  
>> -
>> a this pointer. That didn't work.
>
> Only stuff that's immutable or implicitly convertible immutable can be  
> passed
> across threads using spawn and send. Otherwise, there's a risk of it  
> ending up
> getting altered by both threads (or altered by one when the other one is  
> using
> at it). Sometimes, that can be a bit restrictive (particularly when you  
> _know_
> that something isn't going to be altered by the thread sending it after  
> its
> sent but the compiler doesn't), but it avoids all kinds of problems. If  
> you
> want to send a class object across, then it needs to be immutable (which  
> tends
> to be a bit of a pain to do for classes, since they need to have an  
> immutable
> constructor, which is often a pain to do).

It could be that assumeUnique should handle this, by transforming the
delegate into something spawn() and friends could handle.

Might be worth an enhancement request.


-- 
   Simen


More information about the Digitalmars-d-learn mailing list