How would one pass type information into a thread, so objects of the correct types could be created in the thread using the passed info? Something like this:
struct Test // or class Test
{
int x, y, z;
}
void testInThread(F, T ...)(T args)
{
auto obj = F(args);
// Do stuf with obj in the new thread
}
auto tid = std.concurrency.spawn!(testInThread, Test, 1, 2, 3);
// Threads and stuff...
This doesn't compile, but I'm sure something like this should be possible. I think there's just something I'm not understanding about template parameters.
This line here would compile:
auto tid = std.concurrency.spawn(&testInThread!(Test, int, int, int), 1, 2, 3);
I'm not sure if you can make it prettier with implicit deduction of those ints or not though. But the reason this compiles is that spawn
expects a function. testInThread
is a template that generates a function. If you pass it the compile time argument list over there without a runtime list, you can get the address to the function... which is good enough for spawn
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With