|
-
Jan 19th, 2002, 04:05 PM
#1
Thread Starter
Frenzied Member
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.
-
Jan 19th, 2002, 04:31 PM
#2
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.
-
Jan 19th, 2002, 04:33 PM
#3
Thread Starter
Frenzied Member
cheers zaei, will have a look their in a tick. i have been working on server stuff recently as you may have noticed 
i probably will need a demo coz im that constipated.
-
Jan 20th, 2002, 06:09 AM
#4
Monday Morning Lunatic
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);
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 20th, 2002, 07:28 AM
#5
Thread Starter
Frenzied Member
lol, i got this going now but i used a CreateThread version which works.
-
Jan 20th, 2002, 08:07 AM
#6
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 20th, 2002, 12:10 PM
#7
-
Jan 20th, 2002, 03:42 PM
#8
Thread Starter
Frenzied Member
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.
-
Jan 20th, 2002, 04:28 PM
#9
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 20th, 2002, 04:41 PM
#10
Thread Starter
Frenzied Member
lol, i changed the threading code to what you said, then i changed the project settings and now the server dont work properly lol
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|