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
Allows passing by reference through spawn* if is a shared type #7124
Conversation
Thanks for your pull request and interest in making D better, @ErnyTech! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + phobos#7124" |
5f0f455
to
eec972c
Compare
no one wants to review this? |
CC @wilzbach |
@atilaneves Now do you think it can be approved? |
circleci has failed for |
If you rebase to master the error should go away. I fixed it in #7160. |
It is often useful to pass a mutable by reference type to the spawned thread, the need to have to use a raw pointer before this patch made the syntax boring and not in line with the rest of the DLang style (use pointer as little as possible). Signed-off-by: Ernesto Castellotti <erny.castell@gmail.com>
Ok now circleci is green, thanks! |
I think the pointer-based syntax has the advantage of signifying to the reader that the spawned function will access by reference the arguments. With concurrency one should be extra careful about the objects' lifetime. Consider the following: void main()
{
int var;
f();
}
void f()
{
import std.concurrency : spawn;
// run asynchronously, without immediately awaiting it to finish
shared(int) number = 10;
spawn(&g, number); // passed by value before
// after: accesses an expired stack frame from another thread
}
void g(ref shared(int) number)
{
import core.atomic : atomicOp;
atomicOp!"+="(number, 1);
} |
I do not believe that a reader has particular difficulties in understanding what is happening when he reads "ref" in the parameters |
This seems to result in a consistent failure in excel-d.
|
How about making a spawnRef function to make everyone happy and avoid any kind of code breaks? |
I believe there is a logical error in excel-d, this function (https://github.com/kaleidicassociates/excel-d/blob/91034ca039979f945828820e2eafd817963a7a1e/source/xlld/wrap/wrap.d#L544) contains ref in the parameters but we know that before this PR could not be passed by ref with spawn, in my opinion this is what causes the test to fail However, this keeps me thinking that maybe spawnRef is the best solution, at least you don't risk breaking code |
I'd be okay with having a |
Alternative PR: #7167 |
I think that spawn should work with functions that take their arguments by ref. It's a limitation that doesn't make any sense to me at the moment. I don't think that explicitly adding a |
@atilaneves any advice to solve the problem with excel-d? |
@atilaneves what about the example I provided in #7124 (comment)? We can't allow such brutally unsafe things, while other languages such as Rust focus on concurrency safety, in addition to memory safety. |
@ErnyTech I haven't had time to look into it. I'll try tomorrow but if not then I'm on holiday for 3 weeks. |
@ZombineDev How is the Lifetime problems need to be dealt with the same way regardless if an argument is |
Filed: https://issues.dlang.org/show_bug.cgi?id=21848 Closing. |
It is often useful to pass a mutable by reference type to the spawned
thread, the need to have to use a raw pointer before this patch made
the syntax boring and not in line with the rest of the DLang style
(use pointer as little as possible).
With this patch it will be possible to write code like this:
Instead of the boring: