How does calling function pointers work?
Mike Parker
aldacron at gmail.com
Mon Nov 12 16:54:55 UTC 2018
On Monday, 12 November 2018 at 16:29:24 UTC, helxi wrote:
>
>
> Looks like worker needs an int and spawn(&worker, i * 10) seems
> to feed it's second arg to worker(?)
spawn is a template that takes a function pointer and a variable
number of parameters. Both the pointer and the parameters are
passed on to an internal _spawn function.
https://github.com/dlang/phobos/blob/master/std/concurrency.d#L446
_spawn is has the same template parameters as spawn. It has an
internal function that actually makes the call to the function
pointer (fn):
void exec()
{
thisInfo.ident = spawnTid;
thisInfo.owner = ownerTid;
fn(args);
}
https://github.com/dlang/phobos/blob/master/std/concurrency.d#L538
A few lines down from there, a pointer to exec is passed to
either scheduler.spawn or the Thread constructor. When it's
ultimately called, your function will be called in turn.
At any rate, the actual call to the function pointer is fn(args).
More information about the Digitalmars-d-learn
mailing list