Bug 10704 – Cannot pass arguments by ref with std.concurrency.spawn

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-23T09:21:43Z
Last change time
2024-12-01T16:18:23Z
Assigned to
No Owner
Creator
John Colvin
Moved to GitHub: phobos#9992 →

Comments

Comment #0 by john.loughran.colvin — 2013-07-23T09:21:43Z
It can be useful to pass ref arguments when spawning a new thread (with appropriate care of course), but spawn currently doesn't allow that.
Comment #1 by stanislav.blinov — 2014-02-11T15:07:00Z
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.
Comment #2 by robert.schadek — 2024-12-01T16:18:23Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/9992 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB