Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows passing by reference through spawn* if is a shared type
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>
- Loading branch information
Ernesto Castellotti
committed
Aug 8, 2019
1 parent
225a6af
commit eec972c
Showing
2 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Allows passing by reference through spawn* if is a shared type | ||
|
||
Now it will be possible to pass variables to the thread created with spawn/spawnLinked by reference, | ||
to do this you need the shared type and add ref to the attributes of the arguments of the function to be executed. | ||
|
||
--- | ||
import std.concurrency : spawn; | ||
import core.atomic : atomicOp; | ||
import core.thread : thread_joinAll; | ||
|
||
static void f1(ref shared(int) number) | ||
{ | ||
atomicOp!"+="(number, 1); | ||
} | ||
|
||
shared(int) number = 10; | ||
spawn(&f1, number); | ||
thread_joinAll(); | ||
assert(number == 11); | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters