I discovered the windows thread function recently and I saw that it can take actual variables and transfer them to the thread while keeping their names and working like a reference. I have tried to recreate it, but I didn't get anywhere.
The example of what I would like to do is:
I don't know anything about the "Windows thread function" (there's no such name that I can find), so I can't guess whether this is supposed to be generic code or in what context function() is supposed to appear.
It would be helpful to provide a link to the API.
The most direct way to get the semantics you asked for is something like the following:
The name of the function is CreateThread, and it is a part of the windows.h header.
Also, I want to be able to accept any amount of variables, not only 4. That's what I don't know how to do. I know that there are ways to pass an undefined amount of variables to a function, but the problem is keeping the name of the variable the same. It was somehow done in the CreateThread function, and that's what I'm trying to find out.
take actual variables and transfer them to the thread while keeping their names and working like a reference
with std::future could be one way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <chrono>
#include <future>
int myFunc(constint& a, constint& b)
{
usingnamespace std::chrono_literals;
std::this_thread::sleep_for(2s);
std::cout << "Waited 2 secs!\n";
return a + b;
}
int main()
{
std::future<int> fut = std::async(std::launch::async, myFunc, 5, 6);
int a = fut.get();
std::cout << a << "\n";
}
CreateThread takes 6 arguments. That is what is seen in the link that you posted.
What are you actually trying to replicate? Which lines of code do you think is doing what you want? When we know what you are looking for we can give you ideas (or possibly explain a misconception you might have).
Sorry, OP, I thought I posted this before, but it looks like something went wrong...just another alternative:
Like the others, I'm still confused about what you want.
Are you perhaps referring to the function called indirectly? That one accepts only one parameter.
You can implicitly bind names to references in a closure, and invoke the closure (either asynchronously as @gunnerfunner shows you or otherwise). However, such an object is neither generally thread-safe nor (directly) usable by Win32's CreateThread(); you'd need to write a thunk at very least.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# include <iostream>
template <typename Fn>
auto contrived_invoke(Fn f)
{ return std::forward<Fn>(f)(); }
template <typename Fn, typename... Args>
auto contrived_invoke(Fn&& f, Args&&...args) {
return std::forward<Fn>(f)(std::forward<Args>(args)...);
}
int main() {
int a{}; double b{}; char c{}; /* ... */
auto fn = [&]{ a++; b = 42.0; c = 'a'; };
contrived_invoke(fn); // call fn later, maybe asynchronously
std::cout << a << ", " << b << ", " << c << "\n";
}
I havent worked with the win threads in a while (we pulled p-thread library in instead for portability) but as I recall it packs all the variables you want to send to the thread in a void pointer . This format you posted is strange to me.