i want to do this simple piece of code work.
#include <iostream> #include <windows.h> void printSome (int i) { std::cout << i << std::endl; } void spawnThread (void (*threadName)(int i)) { CreateThread ( 0, // default security attributes 0, // use default stack size (LPTHREAD_START_ROUTINE)threadName, // thread function name (LPVOID)i, // argument to thread function 0, // use default creation flags 0 // returns the thread identifier ); } int main () { spawnThread(printSome(155)); }
i am on windows, using vs. Any help will be greatly appriciated.
Personally, I wouldn’t consider passing in a function pointer like you are trying to do as very C++ like. That’s coding C in C++
Instead, I’d wrap that thing in a class. The big advantage there is you can just override the class to have however many members you want, rather than having to perform greazy casting tricks to get at your parameters every time.
The code’s a little long-winded, so I pushed it to the end. But what it lets you do is something like this:
Here’s some exerpted example code from one of our classes that does this:
…and in the .cpp: