Passing actual variables to a function

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int function(something){
   var1 = 1;
   var2 = 2;
   var3 = 3;
   var4 = 4.0;
}

int main(){
   int var1, var2, var3;
   double var4, sum;
   function(var1, var2, var3, var4);
   sum = var1 + var2 + var3 + var4;
   std::cout << sum;
}


I'm using MinGW on Windows 10.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# include <iostream>
void function(int& var1, int& var2, int& var3, double& var4) {
   var1 = 1;
   var2 = 2;
   var3 = 3;
   var4 = 4.0;
}

int main(){
   int var1, var2, var3;
   double var4, sum;
   function(var1, var2, var3, var4);
   sum = var1 + var2 + var3 + var4;
   std::cout << sum;
}

http://coliru.stacked-crooked.com/a/cdc0de219c469ebb
Last edited on
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.

Here's the link to Microsoft's webpage: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682516(v=vs.85).aspx

Last edited on
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(const int& a, const int& b)
{
    using namespace 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";
}
Thank you. I haven't used C++11 before, but I'll give it a try.
@OP,

I'm a little bit confused. According to the CreateThread help page (first link from the page you provided),

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453(v=vs.85).aspx

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";
}


Live demo:
http://coliru.stacked-crooked.com/a/3ce689b3b53f8fa7
Last edited on
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.
You can create a struct/class to store your variables and pass that to CreateThread(). Not sure how correct/good my code is but it seems to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <Windows.h>

struct Args
{
    static constexpr std::size_t ARR_SIZE{ 3 };
    int var[ARR_SIZE];
    double vard;
};

void function( Args* const args )
{
    for( std::size_t i{}; i < Args::ARR_SIZE; i++ )
        args->var[i] = i + 1;
    args->vard = 4.0;
}

int main()
{
    Args a;
    HANDLE thread = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)function, &a, 0, NULL );
    WaitForSingleObject( thread, INFINITE );
    double sum{};
    for( std::size_t i{}; i < Args::ARR_SIZE; i++ ) sum += a.var[i];
    sum += a.vard;
    std::cout << "sum = " << sum << '\n';
    CloseHandle( thread );
}


sum = 10
Topic archived. No new replies allowed.