Site icon Learn C++

Learn Useful Bidirectional Fences In Modern Multi-threading C++ Apps

Learn Useful Bidirectional Fences In Modern Multi threading C++ Apps

C++11 improved the support for bidirectional fences in multi-thread applications. In modern C++ developmentFences are synchronization primitives in multi-threading operations, they are memory barriers in threads, and they can acquire semantics, release semantics, or both. In this post, we explain what are fences and how we can use them.

What are bidirectional fences in C++?

A fence is a primitive that enforces ordering between preceding loads or stores and subsequent loads or stores. C++11 improves the support for bidirectional fences. Fences are synchronization primitives in multi-threading operations they can have acquire semantics, release semantics, or both of them. A fence with acquire semantics is called an acquire fence and a fence with release semantics is called a release fence. If both operations are done, we call them full fence

In modern C++, std::atomic_thread_fence is called fences, they are memory barriers in multi-thread operations, and they establish synchronization and ordering constraints between each thread without any atomic operation. In other words, std::atomic_thread_fence establishes memory synchronization ordering of non-atomic and relaxed atomic accesses without an associated atomic operation.

How to use bidirectional fences in C++?

Fences are useful between load and store operations and there are 4 types .

Here is the syntax how we can use std::atomic_thread_fence,

[crayon-6763925b06806782000657/]

Here are two simple examples how to use in release and acquire operations of thread functions,

[crayon-6763925b06811119050257/]
[crayon-6763925b06813022815155/]

according to open-std.org document , depending on the value of memory_order, this operation it has different effects,

Is there a full C++ example of how to use bidirectional fences in C++?

Let’s assume we have two threads one is writing to data other one is reading

[crayon-6763925b06815846139547/]

In different runs, you will receive different R and A prints, for example,

[crayon-6763925b06817782549960/]

In this example above, we want all operations to be done in the first thread, thus we can use std::atomic_thread_fence as below,

[crayon-6763925b06818931529911/]

here is the output,

[crayon-6763925b0681b631602102/]

In C++, there is another fence option, std::atomic_signal_fence is another fence type that synchronizes between a signal handler and code running on the same thread. This will be explained in another post.

For more information about bidirectional fences, please see Bidirectional Fences Proposal document.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Exit mobile version