I’m trying to learn how create new threads and run them. I need to pass a few variables into the function that is run on the new thread but I can’t find out how to actually pass anything to that new function/thread.
I’m following http://www.devarticles.com/c/a/Cplusplus/Multithreading-in-C/1/ but it only goes through how to pass a single parameter and nothing else.
Side question, do threads work the exact same way as functions do except just on a different thread or is it a little more complicated than just that?
Thanks,
-Faken
The underlying OS allows to pass only one parameter to a thread CreateThread:
Accordingly the CRT thread create function allows also only one parameter, despite the name being
arglist:Given these restrictions the usual convention is to pass a pointer to a structure/class with all the arguments. Usually, with C++, one creates a static function that will be the thread handler and passes an instance as the argument:
This is a fairly common idiom and in effect it allows you to pass as much state (=arguments) as needed to the thread.