What does java.lang.Thread.interrupt() do?

Thread.interrupt() sets the interrupted status/flag of the target thread. Then code running in that target thread MAY poll the interrupted status and handle it appropriately. Some methods that block such as Object.wait() may consume the interrupted status immediately and throw an appropriate exception (usually InterruptedException) Interruption in Java is not pre-emptive. Put another way both threads have to cooperate … Read more

Multiprocessing vs Threading Python

The threading module uses threads, the multiprocessing module uses processes. The difference is that threads run in the same memory space, while processes have separate memory. This makes it a bit harder to share objects between processes with multiprocessing. Since threads use the same memory, precautions have to be taken or two threads will write to the same memory … Read more

Timeout on a function call

You may use the signal package if you are running on UNIX: 10 seconds after the call signal.alarm(10), the handler is called. This raises an exception that you can intercept from the regular Python code. This module doesn’t play well with threads (but then, who does?) Note that since we raise an exception when timeout happens, it may end … Read more

Thread pooling in C++11

This is copied from my answer to another very similar post: Start with maximum number of threads the system can support:int num_threads = std::thread::hardware_concurrency(); For an efficient threadpool implementation, once threads are created according to num_threads, it’s better not to create new ones, or destroy old ones (by joining). There will be a performance penalty, an … Read more