# Ongoing Optimization

Contents[Show]

Now it's time to put the theory into practice. The job is quite easy. A small program should undergo an ongoing optimization.

## The program

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24``` ```// ongoingOptimization.cpp #include #include int x= 0; int y= 0; void writing(){ x= 2000; y= 11; } void reading(){ std::cout << "y: " << y << " "; std::cout << "x: " << x << std::endl; } int main(){ std::thread thread1(writing); std::thread thread2(reading); thread1.join(); thread2.join(); } ```

The program is quite simple. It consists of two threads thread1 and thread2.thread1 writes the values x and y. thread 2 reads the values x and y in the opposite direction.  The execution of the program is not so thrilling either.

But you see, even this simple program has different results. So I finally come to my key idea.

In the process of the ongoing optimization of the program, I have two questions in my mind.

1. Is the program well defined? Precisely, is there a data race?
2. Which values for x and y are possible?

The first question is often very challenging to answer. So I will reason in the first step about the program and I will verify in the second step my reasoning with CppMem.  If I have answered the first question, the second answer can easily be derived. I will provide the possible values in a table.

But still, one question isn't answered yet? What do I mean by ongoing optimization? I mean by ongoing optimization, that I try to improve the program by weakening the C++ memory model. So in my journey, I discuss the following stations.

• Non-atomic variables
• Locks
• Atomics with sequential consistency
• Atomics with acquire-release semantic
• Atomics with relaxed semantic
• Volatile variables

## What's next?

Of course, this was a very short post. But this post should provide you only the context of my ongoing optimization. In case you are bored, reason about the small program and answer the two question.

1. Is the program well defined?
2. Which values for x and y are possible?

Still bored? Then reason about the weakening of the memory model in the program and about the consequences. In the next post, I will analyse the unsynchronized access with the help of CppMem. Stay tuned.

Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Ralf Holly, Juan Dent, George Liao, and Daniel Ceperley.

Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, John Nebel, and Mipko.

My special thanks to PVS-Studio

## Mentoring Program

My new mentoring program "Fundamentals for C++ Professionals" starts on the 22nd of April. Get more information here: https://bit.ly/MentoringProgramModernesCpp.

## Seminars

I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

### Standard Seminars (English/German)

Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.

### Modernes C++,

0 #1 http://s600.net 2016-10-02 01:39
I enjoy the efforts you have put in this, appreciate it for
all the great posts.
0 #2 datablog 2016-10-05 00:14
gossips and internet and this is actually frustrating.
A good web site with interesting content, that is what I need.
Thank you for keeping this web site, I will be visiting it.
Do you do newsletters? Cant find it.
0 #3 Paul 2016-10-25 19:26
You have remarked very interesting points! ps nice web site.
0 #4 Photos For instagram 2016-10-29 01:54
I don't commonly comment but I gotta admit appreciate it
for the post on this perfect one :D.
0 #5 http://Iprps.mx 2016-11-16 12:32
Really wonderful visual appeal on this internet
site, I'd rate it 10 10.
0 #6 datenschmutz weblog 2016-11-18 19:26
Hello I am so grateful I found your site, I really found you
by mistake, while I was looking on Aol for something else, Anyways I am here now and would just like to say
thank you for a incredible post and a all round exciting blog (I
also love the theme/design), I don't have time to go through it all at the moment but I have book-marked it
and also included your RSS feeds, so when I have time I will be back to read
much more, Please do keep up the superb
job.
0 #7 Weldon 2016-12-03 16:05
Appreciate it for this wonderful post, I am glad I found this web site on yahoo.

### Subscribe to the newsletter (+ pdf bundle)

 Name Email Please enable the javascript to submit this form

### Visitors

Today 464

Yesterday 6623

Week 7087

Month 145479

All 9425502

Currently are 143 guests and no members online

• #### Define Concepts

Thanks. I see the issue. A function cannot have different return types. I wanted to make it simple ... ...

• #### Mixins

I will write about C++23 upcoming posts.

• #### More about Dynamic and Static Polymorphism

Thanks. Fixed

• #### A std::advance Implementation with C++98, C++17, and C++20

I intentionally used the restricted type parameters and not the abbreviated function templates syntax.

• #### Define Concepts

I hate to say that, but your code has a bug. Running function gcd with an integral type smaller than ...