I recently started to look into using multithreading in my application for doing some calculations in the background. The problem I am having is with using the std::thread class for creating threads with functions where I want to pass parameters.
According to most tutorials I have read on the matter the syntax for initializing a thread with a function that uses multiple arguments should be like this
1 2 3 4 5 6 7 8
|
void func(int x, int y)
{
//Do something
}
int c = 3;
int z = 4;
std::thread My_Thread(func,c,z)
My_Thread.join();
|
My code looks somewhat similar, although I am initializing a whole array of threads using a for statement. My code looks like this
1 2 3 4 5 6 7 8 9 10
|
Vector2D World_Quarts[8];
std::thread Water_Quad_Update[4];
for(int i = 0; i < 4; i++)
{
Water_Quad_Update[i] = std::thread(Update_Water_Quadrant,World_Quarts[i*2],World_Quarts[i*2 + 1]);
}
Water_Quad_Update[0].join();
Water_Quad_Update[1].join();
Water_Quad_Update[2].join();
Water_Quad_Update[3].join();
|
where Update_Water_Quadrant is a function that looks like this
1 2 3 4
|
void Update_Water_Quadrant(Vector2D start, Vector2D Quad_Dim)
{
//Update water
}
|
Now this should work, but instead the compiler spits an error C3867(function call missing argument list) and an error C2661(std::thread::thread no overloaded function takes 3 arguments).
Don't know if it is relevant, but all of this is done within a single class(the function creating the std::threads is a part of the World_Manager class as is the Update_Water_Quadrant function).
I'm using Visual Express C++ 2012.