FreeBASIC currently provides ThreadCreate and ThreadWait -- I wonder if it's possible to add ThreadDestroy?
My app was originally like this:
* start
* run
* stop
Now it's:
* start
* run
* wait_for_x
* run
* stop
So I'm trying to put wait_for_x to a Thread so now it's:
* start
* run (and) wait_for_x
* stop
But the problem is my app will proceed to stop but will NOT end since the Thread (wait_for_x) is running.
I was thinking of force exiting the thread but don't have an idea how.
~creek23
How to kill a Thread
Re: How to kill a Thread
At least under Windows the thread ends when the thread procedure returns.
Re: How to kill a Thread
But it doesn't when the procedure is blocked.
The code above is preventing the app from closing.
Btw, I'm trying to make the wait_for_x to look generic since I believe some will also get into this same problem I'm facing. But just to make it a little clearer, my wait_for_x is NetAccept function from Joshy's fbNet library. I've already looked into the code and this NetAccept is actually an alias from an Accept function inside ws2_32 library.
~creek23
Code: Select all
sub mythread(p as any ptr)
wait_for_x
end sub
Btw, I'm trying to make the wait_for_x to look generic since I believe some will also get into this same problem I'm facing. But just to make it a little clearer, my wait_for_x is NetAccept function from Joshy's fbNet library. I've already looked into the code and this NetAccept is actually an alias from an Accept function inside ws2_32 library.
Code: Select all
Declare Function NetAccept Alias "accept"( _
Byval hSocket As FDSOCKET, _
Byval lpAddr As sockaddr_in Ptr, _
Byval lpAddrSize As Integer Ptr) As Integer
Re: How to kill a Thread
Generally speaking killing a thread is considered dangerous, unsafe operation that should not be done (and FB library doesn't provide a way to do it). The way these are normally handled is that a blocking function has a timeout after which it returns, so with threading, you would call a blocking function in a loop, with e.g. 1 second time-out and then check if an end condition has been met (e.g. main thread has set some variable) - if so, exit the loop and the thread procedure thus ending the thread.
Browsing through the MSDN winsocket accept() function reference, you should make the socket you are accepting connections for non-blocking, in which case the function will return with an error if there is no pending connection - then just try again in a loop after a short delay (provided main thread has not requested a shutdown by e..g setting some magic variable).
How you make a socket non-blocking in the Net lib? That you'll need to find out yourself.
Browsing through the MSDN winsocket accept() function reference, you should make the socket you are accepting connections for non-blocking, in which case the function will return with an error if there is no pending connection - then just try again in a loop after a short delay (provided main thread has not requested a shutdown by e..g setting some magic variable).
How you make a socket non-blocking in the Net lib? That you'll need to find out yourself.
Re: How to kill a Thread
Regarding your particular situation, accept() stopping the thread from closing, you can close the socket from another thread which will cause accept() to return, all be it with an error. Although from what I've read I believe this is considered safe, but I could be wrong. It works for me at least (on win7).