Question: I have a third party library. This library has function named `int foo()`. Function is thread based and I cannot change the content of the function. (It does not belong to me.)

When I call the function, it becomes locked and does not return the value. Is there any way to kill this thread based function, when the function is locked? For example, when the function doesn't return the value within 5 seconds, I want to kill it without any memory leak.

Answer: 
Since its a third party library which you have no control over, you cannot portably terminate the thread that runs that code, though you can call for the `native_handle` and use its thread termination facilities, you will most likely introduce **leaks**.

Note that, threads live in the same Address space, hence a corruption or a leak from one thread affects your entire program.

The option I can think of is to spawn a new process to run that code, if after 5 seconds it doesn't complete, you can request the OS to kill it. {No memory leaks and resources are freed} :-) ...your best choice...
No, there is no way to do so without memory leaks, because the thread running `foo` may allocate heap data, might put some private data inside values owned by your main program or some other thead.

Notice that data liveness (and [virtual address space](https://en.wikipedia.org/wiki/Virtual_address_space)) is a whole-program property: some heap data does not belong to a (particular) thread, but to the whole process. The library could (and probably should) use smart pointers as a *convention*.