Skip to content

Added spawnRef and spawnLinkedRef for passing by reference through spawn#7166

Closed
ghost wants to merge 0 commit intodlang:masterfrom
e-archive56:spawnRef
Closed

Added spawnRef and spawnLinkedRef for passing by reference through spawn#7166
ghost wants to merge 0 commit intodlang:masterfrom
e-archive56:spawnRef

Conversation

@ghost
Copy link

@ghost ghost commented Sep 1, 2019

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:

import std.concurrency : spawnRef;
import core.atomic : atomicOp;
import core.thread : thread_joinAll;

static void f1(ref shared(int) number)
{
  atomicOp!"+="(number, 1);
}

shared(int) number = 10;
spawnRef(&f1, number);
thread_joinAll();
assert(number == 11);

Instead of the boring:

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 PR is an alternative of #7124

@ghost ghost closed this Sep 1, 2019
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants