The right way is to create a βkill meβ event using CreateEvent, and then mark this event when you want to kill the stream. Instead of waiting for the while(1) stream, wait [t21>. And then you can just enable the thread callback and return, no need to call _endthreadex or such.
Example callback function:
static DWORD WINAPI thread_callback (LPVOID param) { ... while(WaitForSingleObject(hevent_killme, 0) != WAIT_OBJECT_0) {
Subscriber:
HANDLE hevent_killme = CreateEvent(...); ... void killthread (void) { SetEvent(hevent_killme); WaitForSingleObject(hthread_the_thread, INFINITE); CloseHandle(hevent_killme); CloseHandle(hthread_the_thread); }
Never use TerminateThread.
source share