Can a hyper-threaded processor core execute two threads at the exact same time?

See my answer on a softwareengineering.SE question for some details about how modern CPUs find and exploit instruction-level parallelism (ILP) by running multiple instructions at once. (Including a block diagram of Intel Haswell’s pipeline, and links to more CPU microarchitecture details). Also Modern Microprocessors A 90-Minute Guide! You have a CPU with lots of execution … Read more

How does x86 pause instruction work in spinlock *and* can it be used in other scenarios?

PAUSE notifies the CPU that this is a spinlock wait loop so memory and cache accesses may be optimized. See also pause instruction in x86 for some more details about avoiding the memory-order mis-speculation when leaving the spin-loop. PAUSE may actually stop CPU for some time to save power. Older CPUs decode it as REP … Read more

When is a condition variable needed, isn’t a mutex enough?

Even though you can use them in the way you describe, mutexes weren’t designed for use as a notification/synchronization mechanism. They are meant to provide mutually exclusive access to a shared resource. Using mutexes to signal a condition is awkward and I suppose would look something like this (where Thread1 is signaled by Thread2): Thread1: … Read more

Are incrementers / decrementers (var++, var–) etc thread safe?

No, incrementing is not thread-safe. Neither are the INC and DEC instructions. They all require a load and a store, and a thread running on another CPU could do its own load or store on the same memory location interleaved between those operations. Some languages have built-in support for thread synchronization, but it’s usually something … Read more

How do I Understand Read Memory Barriers and Volatile

There are read barriers and write barriers; acquire barriers and release barriers. And more (io vs memory, etc). The barriers are not there to control “latest” value or “freshness” of the values. They are there to control the relative ordering of memory accesses. Write barriers control the order of writes. Because writes to memory are … Read more

Asynchronous IO in Scala with futures

Use Futures in Scala 2.10. They were joint work between the Scala team, the Akka team, and Twitter to reach a more standardized future API and implementation for use across frameworks. We just published a guide at: http://docs.scala-lang.org/overviews/core/futures.html Beyond being completely non-blocking (by default, though we provide the ability to do managed blocking operations) and … Read more

Are “data races” and “race condition” actually the same thing in context of concurrent programming

No, they are not the same thing. They are not a subset of one another. They are also neither the necessary, nor the sufficient condition for one another. The definition of a data race is pretty clear, and therefore, its discovery can be automated. A data race occurs when 2 instructions from different threads access … Read more

How to configure a fine tuned thread pool for futures?

This answer is from monkjack, a comment from the accepted answer. However, one can miss this great answer so I’m reposting it here. implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10)) If you just need to change the thread pool count, just use the global executor and pass the following system properties. -Dscala.concurrent.context.numThreads=8 -Dscala.concurrent.context.maxThreads=8