Passing arguments to a thread
Like in functions, we may also want to send parameters and arguments to the thread. As threads are just tasks, and tasks are just a collection of functions, it is necessary to understand how to send arguments to a thread. If we can send arguments to a thread at runtime, then the thread can perform all the operations dynamically. In most cases, we would thread the physics, AI, or audio sections of the code. All these sections would require functions that take in arguments.
Getting ready
You need a Windows machine and a working copy of Visual Studio. No other prerequisites are required.
How to do it…
In this recipe, we will find out how easy it is to add a heuristic function to our game for pathfinding. Add a source file called Source.cpp
. Add the following code to it:
class Wrapper { public: void operator()(std::string& msg) { msg = " I am from T1"; std::cout << "T1 thread initiated" << msg << std::endl; } }; int main() ...