-
Multithreading
i need some code that allows me to use the CreateThread API function to spin off a new thread and run a function. i also need to know how i should properly declasre the function if it has to be different from normal. i also need to know how to close the thread and delete it.
-
I hear that you should use beginthread instead of CreateThread(). There should be info in the MSDN. I will try to give an example later, if you still need it.
Z.
-
cheers zaei, will have a look their in a tick. i have been working on server stuff recently as you may have noticed :D
i probably will need a demo coz im that constipated.
-
From my current code:
Code:
class thread {
public:
thread(void (__cdecl *func)(void*) = thread::run, void *param = 0);
static void sleep(uint32 milliseconds);
private:
static void run(void *param);
uint32 m_thread;
};
Code:
thread::thread(void (__cdecl *func)(void*), void *param) {
m_thread = _beginthread(func, 0, param);
if(!m_thread) {
throw std::runtime_error("Could not create thread");
}
}
void thread::run(void *param) {
_endthread(); // exit
}
void thread::sleep(uint32 milliseconds) {
return Sleep(milliseconds);
}
-
lol, i got this going now but i used a CreateThread version which works.
-
If you use anything from the Standard Library (whether C or C++) you need to use _beginthread (or _beginthreadex) otherwise you'll get a memory leak.
-
-
ok i changed it to _beginthread and to work like you have done in your code, the only problem is that it cannot find the functions.
i added process.h to includes but still no luck, i looked in the h and saw it had #if defined _MT so i then added #define _MT before the include and it says it cannot link to _beginthread whereas endthread works fine.
-
Take out your #define _MT, and in Project Settings you need to change it from Single threaded to Multithreaded (under C++ I think).
You'll need to change it for both Debug and Release.
-
lol, i changed the threading code to what you said, then i changed the project settings and now the server dont work properly lol