[Issue 10704] Cannot pass arguments by ref with std.concurrency.spawn

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 11 15:07:05 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=10704


Stanislav Blinov <stanislav.blinov at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |stanislav.blinov at gmail.com


--- Comment #1 from Stanislav Blinov <stanislav.blinov at gmail.com> 2014-02-11 15:07:00 PST ---
This is an interesting request.

TDPL states that any ref passing (explicit or implicit) into spawn should be
prohibited, except for immutable. However, current implementation of spawn does
allow passing pointers to shared. But if we can pass pointers, why aren't we
able to pass by ref?

This:

void func(shared int* p) {}
shared int i;
spawn(&func, &i);

is no more safer than this:

void func(ref shared int p) {}
shared int i;
spawn(&func, i);

or even this:

struct Foo { shared int* p; }

void func(Foo foo) {}
shared int i;
spawn(&func, Foo(&i));

Granted, as you've pointed out, without proper care all of those are "Hello,
Segfault!", but how else then to pass around shared data without resorting to
global variables?

ref propagation could be implemented rather easily (I've done this for my
custom thread spawner). It can even be made a bit more involving for the user
(e.g. require to create a NullableRef first, a-la C++):

auto nref(ref T v) { return NullableRef!T(&v); }
spawn(&func, i.nref);

The question is how legal this is. Perhaps Andrei and Sean could shed some
light on this?

I myself voting for this enhancement.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list