Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot pass arguments by ref with std.concurrency.spawn #9992

Open
dlangBugzillaToGithub opened this issue Jul 23, 2013 · 1 comment
Open

Comments

@dlangBugzillaToGithub
Copy link

john.loughran.colvin (@John-Colvin) reported this on 2013-07-23T09:21:43Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=10704

CC List

  • stanislav.blinov

Description

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.
@dlangBugzillaToGithub
Copy link
Author

stanislav.blinov commented on 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.

@LightBender LightBender removed the P4 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants