Issue 10704 - Cannot pass arguments by ref with std.concurrency.spawn
Summary: Cannot pass arguments by ref with std.concurrency.spawn
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-23 09:21 UTC by John Colvin
Modified: 2024-12-01 16:18 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description John Colvin 2013-07-23 09:21:43 UTC
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 Stanislav Blinov 2014-02-11 15:07:00 UTC
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 dlangBugzillaToGithub 2024-12-01 16:18:23 UTC
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