-
June 17th, 2002, 10:55 AM
#1
thread
Hi
I wanna program a client/server-app. Each client should start an own thread on the server.
I've tried already something, but it doesnt work properly. I got some problems with this "_beginthread"-function.
Code:
Socket s;
SOCKET* newConnection = 0;
while(1)
{
newConnection = s.acceptConnection();
Service myService(newConnection);
_beginthread((void (*)(void *))myService.start(), 0, (void * newConnection);
}
Any better ideas ?
Thanks
Flavio
-
June 17th, 2002, 11:41 AM
#2
I'm suprised it even compiles. Your 1st and second arguments look pretty screwed up.
_beginthread((void (*)(void *))myService.start(), 0, (void * newConnection);
What is (void (*)(void *))myService.start(), supposed to mean?
What is this supposed to do? (void * newConnection
What error do you get?
Try looking at the example in MSDN. It is very different from your code.
-
June 17th, 2002, 02:06 PM
#3
ok i've given a look at msdn and i coded it with the "CreateThread"-function. It works fine now, but i've still a problem:
I can compile it, but i cant link it. there's a problem with the parameter "newConnection" which I wanna give to the thread-function:
well, here's the code:
Code:
DWORD WINAPI myThread(LPVOID);
I guess, LPVOID is wrong, but when I try with SOCKET* (which is my parameter I wanna pass) it doesnt work too.
Code:
void
main()
{
DWORD dwThreadId[100];
HANDLE hThread[100];
int i = 0;
while(1)
{
Socket s;
SOCKET* newConnection = 0;
newConnection = s.acceptConnection();
hThread[i] = CreateThread(
NULL, // default security attributes
0, // use default stack size
myThread, // thread function
newConnection, // argument to thread function
0, // use default creation flags
&dwThreadId[i]); // returns the thread identifier
i++;
Sleep(100);
}
DWORD WINAPI myThread(SOCKET* newConnection)
{
Service myService(newConnection);
return EXIT_SUCCESS;
}
ok, thanks very much
Flavio
-
June 17th, 2002, 03:00 PM
#4
You do want to use the LPVOID. Inside your myThread function you will have to use a cast to get the parameter to the variable type that you want.
For example
Code:
DWORD WINAPI myThread(LPVOID newConnection){
Service myService((SOCKET*)newConnection);
return EXIT_SUCCESS;
}
I hope this helps somewhat.
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
|