How to kill a Thread

General FreeBASIC programming questions.
Post Reply
creek23
Posts: 261
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

How to kill a Thread

Post by creek23 »

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
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: How to kill a Thread

Post by MichaelW »

At least under Windows the thread ends when the thread procedure returns.
creek23
Posts: 261
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

Re: How to kill a Thread

Post by creek23 »

But it doesn't when the procedure is blocked.

Code: Select all

sub mythread(p as any ptr)
  wait_for_x
end sub
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.

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
~creek23
Aave
Posts: 128
Joined: Jun 13, 2008 19:55
Location: Helsinki, Finland

Re: How to kill a Thread

Post by Aave »

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.
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Re: How to kill a Thread

Post by pestery »

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).
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: How to kill a Thread

Post by TJF »

@creek23:

You should consider to use TSNE for asynchronous networking (or GLib).
Post Reply