If this is Windows code (say, under MSVC), you can call the TerminateThread () function, and this will kill the thread, but first you need to check if the threads can be killed, if you want to leave corrupted / half-written data :)
Edit: In order to understand the essence and explain how this can be useful in the context of memory leaks, if a thread allocates a heap, then terminating the stream leaves the heap data loose, however, even if you use the heap for a thread, then you can have access to this heap from others threads / processes in the application and release it from outside the thread when you terminate it by calling the TerminateThread function. Or, alternatively, you can design the application so that the heap is supported by another thread, so that the dedicated thread only uses the stack and takes away access to the heap that already exists, then there is no possibility of memory leak in the dedicated thread, even if the thread is clearly killed. You can theoretically also get a heap lock under windows, although there are several ways you could get around this, for example, you could explicitly unlock this part of the heap after you end the stream using a low library system call and insert your own a lock around this area of the heap so that other threads do not remove the lock while it is unlocking - i.e. A double lock that can be locked and unlocked by the parent process, but that any other threads expect to unlock before accessing this area of the heap (if they use it at all). The great thing about threads is that they share a bunch, so there are many ways to solve this problem with or without thread destruction.
source share