3

I have tried to establish an invariant between two atomic counters in one thread while ensuring that this invariant was maintained during read on an other thread without using a mutex.

Nevertheless looking at the code, it appears that I just implemented some sort of locking algorithm using the two atomic counters (with the risk I just messed up).

Is it possible to share an invariant between threads without using a locking strategy?


EDIT: The term invariant may not be adequate.

Let say I have two variables a and b, at some point in the program execution a thread A set a and b to some distinct values, and after that I woukd like that if a thread B load the value a stored by A and then load b, the loaded value of b is the one stored by A and not a value stored later in b.

Oliv
  • 17,610
  • 1
  • 29
  • 72
  • Can [`std::atomic`](http://en.cppreference.com/w/cpp/atomic/atomic) help in you case? – stefaanv Aug 25 '17 at 07:21
  • In fact I used atomic counter. I precise it in the question. Oups in fact I had already said it was atomic counters... – Oliv Aug 25 '17 at 07:22
  • 'Atomic counters' doesn't necessarily mean they're of type std::atomic. I probably don't get what the invariant is that you're trying to achieve. – stefaanv Aug 25 '17 at 07:29
  • 3
    we need some more information. What is the invariant. How, when and by who are the counter written? – bolov Aug 25 '17 at 07:34
  • @bolov Let say I have two variables 'a' and 'b', at some point in the program execution a thread A set 'a' and 'b' to some distinct values, and after that I woukd like that if a thread B load the value 'a' stored by A and then load 'b', the loaded value of 'b' is the one stored by A and not a value stored later in 'b'. – Oliv Aug 25 '17 at 08:17
  • @Oliv please add all the relevant information into the body of the question. Don't rely on comments. They can be easily deleted without notice or history trace. Moreover, one should be able to answer your question just by reading the question without reading comments. – bolov Aug 25 '17 at 08:21
  • Thanks, I ll correct the question soon. – Oliv Aug 25 '17 at 08:58

1 Answers1

4

Is it possible to share an invariant between threads without using a locking strategy?

No.

Why? Imagine the following scenario: Thread X and Y, counters a and b.

Thread X writes into a and b, which causes the invariant to break. Assuming that thread Y attempts to read a and b, while the invariant is broken and without some locking strategy.

At this case thread Y may read stale (or too fresh) data, since a may be the value that X wrote now, or the previous one. Same stands for b.

This is classic bug in multithreading, which results in data races.


It is similar as the double-linked list example discribed here. In that instance, threads X and Y exist and manipulate a double linked list. One of the Invariants is that if we follow a next pointer from one node (1) to another (2), the previous pointer from that node (2) points back to the first node (1).

Assuming that thread X deletes a node of the list, thread Y attemps to read at that point and can cause problems, because there is a chance that the pointers of the previous and next nodes of the node to be deleted by X are not yet set (thus the invariant is broken at the time at thread Y attempts to read the list).

gsamaras
  • 71,951
  • 46
  • 188
  • 305