On Monday, 12 November 2018 at 16:29:24 UTC, helxi wrote:
On Monday, 12 November 2018 at 16:25:13 UTC, Rene Zwanenburg wrote:
Idk where you got that syntax from, but there's no syntactic difference between calling normal functions and function pointers:

import std.stdio;
import std.concurrency;
import core.thread;

void worker(int firstNumber) {
    foreach (i; 0 .. 4) {
        Thread.sleep(500.msecs);
        writeln(firstNumber + i);
    }
}

void main() {
    foreach (i; 1 .. 3) {
        spawn(&worker, i * 10);
    }
}


Looks like worker needs an int and spawn(&worker, i * 10) seems to feed it's second arg to worker(?)

That's right. spawn() is a function in the standard library that takes a function pointer, and all the arguments to pass to that function. It's a bit unusual in that regard: normally when using function pointers the arguments are provided by the code that receives the function pointer. Internally, spawn will call the function pointer just like I did in my example, but on another thread.

Here's an example where a function pointer is passed around with the arguments provided by the callee:
https://run.dlang.io/is/ArCN5t

Reply via email to