Click to See Complete Forum and Search --> : killing a socket thread..


pouncer
April 3rd, 2006, 04:55 PM
I have a thread setup like this..

declared at the top:
HANDLE sock_thread;

This is in a function
sock_thread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)Socket, 0, 0, &sock_thread_id);

which creates a thread on:


void Socket() {
//code here for recived bytes etc
//code here for recived bytes etc
//eg..

if (blah = "ok") { do this; }
else if (blah = "no") { do this; }

Socket();
}


but the app keeps crashing even when i set sock_thread to 0, whats the problem?

MrViggy
April 3rd, 2006, 05:09 PM
Don't know. You need to post complete code.

Viggy

wildfrog
April 3rd, 2006, 06:14 PM
void Socket() {
?
The prototype THREAD_START_ROUTINE is like this:

DWORD WINAPI ThreadFunc(LPVOID pvData)

- petter

MrViggy
April 3rd, 2006, 06:24 PM
Also, why are you recursively calling your thread function?

Viggy

pouncer
April 3rd, 2006, 06:26 PM
but in general anyway, how does one kill a thread?

pouncer
April 3rd, 2006, 06:35 PM
Also, why are you recursively calling your thread function?

Viggy

I need to do this, because this is a dll.

The socket thread recevies the data from the server, so i need to keep recursively calling it.

MrViggy
April 3rd, 2006, 06:42 PM
First, I'd go with an infinite loop, instead of recursive function calls. Recursive functions in general are more trouble then they're worth.

Second, to "kill" a thread, you let the thread function end. For your infinite loop, what you'd want to do is check for some kind of "kill" signal; then just break out of the loop. Something that is somewhat more difficult with a recursive thread function! :D

Viggy

pouncer
April 3rd, 2006, 06:57 PM
well i have this as a dll function to close the socket, and the thread itself..


int WINAPI KillSocket(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
closesocket(sock);
WSACleanup();
sock = 0;
sock_thread = 0;

return 1;
}


but when i call this funtion, the socket closes fine, but the thread doesn't.. so im missing this 'kill' thing.. i cant seem to find anything on google either.

this is how the thread was created

sock_thread = CreateThread(0,0, (LPTHREAD_START_ROUTINE)Socket, 0, 0, &sock_thread_id);


DWORD WINAPI Socket() {
//thread function code here blah blah
}


im just setting sock_thread to 0. Do i have to somehow 'kill' the &sock_thread_id it? im not quite sure, any help would be great, thanks!

pouncer
April 3rd, 2006, 07:29 PM
well i've done some MSDN reading and have found some functions.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecoreos5/html/wce50lrfCreateThread.asp


int WINAPI KillSocket(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
closesocket(sock);
WSACleanup();
sock = 0;
sock_thread = 0;
if (GetExitCodeThread(sock_thread, &sock_thread_id) != 0) {
//i need to use the ExitThread() function here but im not sure how
}
return 1;
}


im not quite sure, how to use the ExitThread function. The documentation says i need to get an 'exit code' which i don't know what is. If anyone can point me in the right direction, it would be great.

wildfrog
April 3rd, 2006, 07:39 PM
Something like this:

bool stop_thread = false;

int WINAPI KillSocket(HWND, HWND, char *data, char *parms, BOOL, BOOL) {
// set stop_thread to true indicating that the thread should end
stop_thread = true;

// clean up socket stuff
closesocket(sock);
WSACleanup();
sock = 0;

// Wait for thread to end
WaitForSingleObject(sock_thread, INFINITE);

// Get result from thread
DWORD exitcode;
GetExitCodeThread(sock_thread, &exitcode);

// Dont set sock_thread to null, that will lead to a resource leak, use CloseHandle
// sock_thread = 0;
CloseHandle(sock_thread);

return 1;
}


DWORD WINAPI Socket()
{
while (!stop_thread) // loop as long as stop_thread is false
{
// do your stuff...
}
return 0;
}

- petter

pouncer
April 3rd, 2006, 08:27 PM
wildfrog, very nice reply, appreciate it thanks.

im just gona give it a shot now!

pouncer
April 3rd, 2006, 08:40 PM
wildfrog, you're preety awesome. Code is perfect. Thanks.

wildfrog
April 3rd, 2006, 08:45 PM
Code is perfectI'm glad to hear that! :thumb:
wildfrog, you're preety awesome.I'm glad to hear that aswell :D :wave:

But, you should add error checking ot your code. That's always a good thing.

- petter

pouncer
April 3rd, 2006, 08:49 PM
what kind of error checking? lol

i've got error checking on the thread function like this:


DWORD WINAPI Socket() {

while (!Stop_Thread) {

memset(&recv_buffer, 0, 4096);
num_recvd = recv(sock, recv_buffer, 1000, 0);

if ((num_recvd != 0) || (num_recvd != SOCKET_ERROR)) {

}
}
}


but no error cheking in the KillSocket function unless you mean what i had previously:

if (GetExitCodeThread(sock_thread, &sock_thread_id) != 0) ?

wildfrog
April 3rd, 2006, 09:21 PM
I just meant it as a general rule. You should know the functions you're using (by reading the documentation). You should know how they work, and what you might expect from them (including possible error situations).

The GetExitCodeThread is one example, WaitForSingleObject is another. For instance, what happens if your socket thread won't 'die'. If that happens you'll have two hanging threads... both you socket thread and your main thread because WaitForSingleObject(..., INFINITE) will wait forever...

- petter

pouncer
April 3rd, 2006, 10:26 PM
yeah i see now. i'll give it a bit more read. Thanks alot.