From 9943f6a8ef74ab3c0ebbd8697b49d6036fbe3015 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 5 Aug 2024 19:25:56 +0200 Subject: [PATCH] Update cpp.md Signed-off-by: Dennis Eichhorn --- standards/cpp.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/standards/cpp.md b/standards/cpp.md index fe3dd31..08bf768 100755 --- a/standards/cpp.md +++ b/standards/cpp.md @@ -36,6 +36,10 @@ When writing code keep the following topics in mind: * atomics vs locking (mutex) * Cache line sharing between CPU cores +### Cache line sharing between CPU cores + +When working with multi-threading you may choose to use atomic variables and atomic operations to reduce the locking in your application. You may think that a variable value `a[0]` used by thread 1 on core 1 and a variable value `a[1]` used by thread 2 on core 2 will have no performance impact. However, this is wrong. Core 1 and core 2 both have different L1 and L2 caches BUT the CPU doesn't just load individual variables, it loads entire cache lines (e.g. 64 bytes). This means that if you define `int a[2]`, it has a high chance of being on the same cache line and therfore thread 1 and thread 2 both have to wait on each other when doing atomic writes. + ## Namespace ### use