Reputation: 10838
I have an application which does some socket communication with some hardwares. Assume for the particular hardware i have an object and this object initiates a thread which listens on a particular port number say 5001 infinitely until a connection is established.
Now if i delete this obect is there anyway by which i can ensure that the thread that is listening on port number 5001 infinitely also gets destroyed.
So the problem is whenever a new object for the same device is created the old thread does not get destroyed and hence there are thread leaks.
Upvotes: 0
Views: 133
Reputation: 62333
Its probably worth setting a variable to say that the thread loop should exit and then sending some data to the socket. This will cause the socket to receive data, wake up, discover its time to exit and exit.
Upvotes: 0
Reputation: 661
You need to use non-blocking socket in this case. In case of blocking sockets, Accept() call blocks until there is a connection. You can use ioctlsocket to make a socket non blocking, and check for error code WSAEWOULDBLOCK from Accept() call. And of course modify your infinite loop to use WaitForSingleObject.
More info here
Upvotes: 1
Reputation: 99665
In Windows you could use WaitForSingleObject function to check whether thread exited (you can pass thread's handle to is as an argument). And you probably want to create event which will initiate thread's exit.
Upvotes: 1