# Inter Thread Latency

DevFeed: [Inter Thread Latency](<https://devfeed.tech/articles/inter-thread-latency-13614.md>)

Original publisher: [Read original article](<https://mechanical-sympathy.blogspot.com/2011/08/inter-thread-latency.html>)

Author: Martin Thompson (noreply@blogger.com)

Published: 2011-08-09T19:37:00Z

Content type: comparison

Language: en

Sources: [Mechanical Sympathy](<https://devfeed.tech/sources/mechanical-sympathy.md>)

Topics: [Latency](<https://devfeed.tech/topics/latency.md>), [Concurrent Programming](<https://devfeed.tech/topics/concurrent-programming.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [Java](<https://devfeed.tech/topics/java.md>), [Cache](<https://devfeed.tech/topics/cache.md>), [cpu](<https://devfeed.tech/topics/cpu.md>)

Tags: [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [cache](<https://devfeed.tech/tags/cache.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [java](<https://devfeed.tech/tags/java.md>), [latency](<https://devfeed.tech/tags/latency.md>), [performance](<https://devfeed.tech/tags/performance.md>), [threads](<https://devfeed.tech/tags/threads.md>)

## AI overview

This article compares C++ and Java for signalling changes between threads. Using a ping-pong test with counters updated by separate threads, it measures cache-line exchange latency between CPU cores. In the reported test, C++ is slightly faster, and the measured latency is approximately 45 nanoseconds on the tested system.

## Source excerpt

Message rates between threads are fundamentally determined by the latency of memory exchange between CPU cores. The minimum unit of transfer will be a cache line exchanged via shared caches or socket interconnects. In a previous article I explained Memory Barriers and why they are important to concurrent programming between threads. These are the instructions that cause a CPU to make memory visible to other cores in an ordered and timely manner. Lately I've been asked a lot about how much faster the Disruptor would be if C++ was used instead of Java. For sure C++ would give more control for memory alignment and potential access to underlying CPU instructions such as memory barriers and lock instructions. In this article I'll directly compare C++ and Java to measure the cost of signalling a change between threads. For the test we'll use two counters each updated by their own thread. A simple ping-pong algorithm will be used to signal from one to the other and back again. The exchange will be repeated millions of times to measure the average latency between cores. This measurement will give us the latency of exchanging a cache line between cores in a serial manner. For Java we'll use volatile counters which the JVM will kindly insert a lock instruction for the update giving us an effective memory barrier. public final class InterThreadLatency implements Runnable { public static final long ITERATIONS = 500L * 1000L * 1000L; public static volatile long s1; public static volatile long s2; public static void main(final String[] args) { Thread t = new Thread(new InterThreadLatency()); t.setDaemon(true); t.start(); long start = System.nanoTime(); long value = s1; while (s1 < ITERATIONS) { while (s2 != value) { // busy spin } value = ++s1; } long duration = System.nanoTime() - start; System.out.println("duration = " + duration); System.out.println("ns per op = " + duration / (ITERATIONS * 2)); System.out.println("op/sec = " + (ITERATIONS * 2L * 1000L * 1000L * 1000L) / durat