Startec

Startec

What's up with this new memory_order_consume memory order? - The Old New Thing

Mai 23, às 06:03

·

3 min de leitura

·

0 leituras

April 27th, 2023 C++20 introduces a new atomic memory order: std::memory_order::consume, more commonly known as std::memory_order_consume, What is this guy? The consume memory order is a weaker form of...
What's up with this new memory_order_consume memory order? - The Old New Thing

April 27th, 2023

C++20 introduces a new atomic memory order: std::memory_order::consume, more commonly known as std::memory_order_consume, What is this guy?

The consume memory order is a weaker form of acquire. Whereas acquire prevents all future memory accesses from being ordered ahead of the load, the consume order only prevents dependent future memory accesses from being reorder ahead of the load.

In all the examples, let’s assume global variables declared and initialized as

int v1 = 1;
int v2 = 2;
std::atomic<int*> p{ &v1 };

Okay, let’s do some consuming.

auto sample_consume()
{
 auto q = p.load(std::memory_order_consume);
 return *q + v2;
}

The compiler is required to read the value from p into q, and any future calculations depending on that value must occur after the load.

This reordering is allowed:

auto sample_consume_allowed()
{
 auto prefetch2 = v2;
 auto q = p.load(std::memory_order_consume);
 return *q + prefetch2;
}

The value of v2 is not dependent on what was loaded from p. Therefore, the compiler and processor are permitted to advance the fetch of v2 ahead of the load of p. Note that an acquire load of p would have prohibited this reordering, since acquire loads block all future memory access, even if unrelated to the value being acquired.

However, this reordering of the above code is not allowed:

auto sample_consume_disallowed()
{
 auto speculate1 = v1;
 auto q = p.load(std::memory_order_consume);
 if (q == &v1) return speculate1 + v2;
 return *q + v2;
}

This speculation lets the code hide the memory latency of accessing v1 behind the load of p, and a compiler might choose to take advantage of this based on profiling feedback, and a processor might do it unilaterally because processors like to do speculative things nowadays. This would be allowed if the load from p were relaxed.

However, the consume memory order prohibits this transformation: The value loaded from p is dereferenced, and that dereference operation is dependent upon the value that was loaded, so the consume memory order requires that the dereference occur after the load.

Here’s a table, because people like tables.

Ordering Relaxed Consume Acquire
Load v2 before p Allowed Allowed Prohibited
Dereference p before load Allowed Prohibited Prohibited

The consume memory order is not used much. Atomic variables are typically tied to other variables in ways that don’t show up in expression dependency graphs, such as for use as mutual exclusion locks. The acquire memory order is much more commonly used than consume.


Continue lendo

DEV

React MUI Content Security Policy
Material-UI is a user interface library that provides predefined and customizable React components for faster and easy web development, these Material-UI components are based on top of Material Design by...

Hoje, às 19:00

Tech Crunch

Taking the pulse on the Northeast seed market with Techstars' Kerty Levy
Techstars’ Kerty Levy knows a thing or two about where seed funding is, and where it might be going, in the Northeast. During a presentation at TechCrunch’s Early Stage in Boston last month, Levy took a brief...

Hoje, às 15:00

Hacker News

Mastering CSS Blend Modes
CSS mix blend modes provide an easy, yet powerful way to create visually interesting designs. Visual effects galore The modes allow you to manipulate how elements interact with each other. Which can lead in...

Hoje, às 14:18

AI | Techcrunch

3 Views on a16z's latest reported early-stage effort
a16z, a venture capital firm known for its large fund sizes and for shaking up the VC game when it piled into the industry back in 2009, is cooking up a new strategy to potentially bolster its deal flow,...

Hoje, às 14:00

TabNews

Dúvida sobre os "níveis" de experiência · Vnj
Olá galera, estou estudando programação ja faz uns meses e me veio a seguinte dúvida: o que te faz ser um júnior? Pelo que li em alguns posts daqui, seria ter algum contato com o mercado...

Hoje, às 13:15

Hacker News

ARM’s Cortex A53: Tiny But Important
Tech enthusiasts probably know ARM as a company that develops reasonably performant CPU architectures with a focus on power efficiency. Product lines like the Cortex A7xx and Cortex X series use we…

Hoje, às 09:12

Mashable

Save up to 70% on cables, power stations, and more in this Memorial Day sale
This is a great time to upgrade your charging setup. The following content is brought to you by Mashable partners. If you buy a product featured here, we may earn an affiliate commission or other...

Hoje, às 09:00

Tech Crunch

Startups should absolutely work with governments to support defense projects
Maëlle Gavet is the CEO of Techstars and was previously a senior executive at numerous large tech companies around the world. In these times of heightened tensions and global volatility, I believe startups...

Hoje, às 08:30

DEV

How to Track Gumroad Sales in Notion Using Notion API and Python
Introduction In this tutorial, you’ll learn how to track Gumroad1 sales in real-time in Notion2 using 🐍 Python. You will also learn, What are APIs? How to use Gumroad API? How to use Notion API? How run a...

Hoje, às 04:24

TabNews

Como criar um git/github (e as primeras configs) obs: no windows e com o vscode · NicolasdevNx
Olá, este "artigo" tem como objetivo ensinar como baixar e usar o git eo o github(para este não é neseçario o dowload) então vomos lá. 1:Acesse o site https://git-scm.com/downloads escolh...

Hoje, às 02:32