Concurrency: Atomic and volatile in C++11 memory model

Firstly, volatile does not imply atomic access. It is designed for things like memory mapped I/O and signal handling. volatile is completely unnecessary when used with std::atomic, and unless your platform documents otherwise, volatile has no bearing on atomic access or memory ordering between threads. If you have a global variable which is shared between … Read more

What is the difference between asynchronous programming and multithreading?

Your misunderstanding is extremely common. Many people are taught that multithreading and asynchrony are the same thing, but they are not. An analogy usually helps. You are cooking in a restaurant. An order comes in for eggs and toast. Synchronous: you cook the eggs, then you cook the toast. Asynchronous, single threaded: you start the … Read more

How do I parallelize a simple Python loop?

Using multiple threads on CPython won’t give you better performance for pure-Python code due to the global interpreter lock (GIL). I suggest using the multiprocessing module instead: pool = multiprocessing.Pool(4) out1, out2, out3 = zip(*pool.map(calc_stuff, range(0, 10 * offset, offset))) Note that this won’t work in the interactive interpreter. To avoid the usual FUD around … Read more

How to wait for all threads to finish, using ExecutorService?

Basically on an ExecutorService you call shutdown() and then awaitTermination(): ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(…) { taskExecutor.execute(new MyTask()); } taskExecutor.shutdown(); try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { … }