1

I have the following situation.

I have a thread that writes to an array. After writing to that array I need to read that array. Reading happens after writing the array and I can guarantee so through another means (this would imply I will not need condition variables, but just when reading "refreshing" contents of the array). The array is read later from another thread.

EDIT: Additionally, the writing will happen at different times, the array is not written all at once.

I need to read the full contents of the array with all entries already refreshed and with the actual values at the time of reading. Right now the values are not refreshed.

What is the best way to synchronize this?

  • mutex?
  • atomic?
  • fences?

I am not sure how I would do it. I do know for a single variable a mutex would be enough, but this is an array. I am not sure what the correct way to do this is. I am not sure how to do it: a lock?

11
  • If you are sure that no more writes will be done to the array, no synchronization at all is needed. Multiple readers only don't need protections. Commented Jun 22, 2016 at 3:00
  • No more writes will be done, but I do not see the data updated from the reader thread, so I assume I am missing some synchronization. Commented Jun 22, 2016 at 3:30
  • For simplicity, I recommend you use mutex. You lock when start to writte in the array and unlock when finish and the same for reading the array. Commented Jun 22, 2016 at 3:35
  • 1
    Using std::atomic_flag is easy too. You clear the flag when finish to writte the array and test_and_set the flag when start to read the array. Commented Jun 22, 2016 at 3:42
  • 1
    @DaveM. Yes German commented: After writing to that array I need to read that array, the unique condition is that reading is done from another thread and don't do writting and reading in parallel. But std::condition_variable is a good choice too. Commented Jun 22, 2016 at 4:44

2 Answers 2

2

There is a fundamental issue that an array cannot be written to and read atomically. Without blocking the writer and the reader may race, so that the reader may observe a partially updated array.

One solution would be to use a single-producer-single-consumer ring-buffer with elements being pointers to those arrays. This way the writer and the reader do not race writing/reading the same array and the reader only ever observes consistent data.

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand the question correctly, you know that you read some values AFTER another thread changed the value, but you don't see the change.

In fact it is not guaranteed that your reading thread will read the updated value of the writing thread without some synchronisation mechanism (you can read more here).

A solution that would work is to use atomic variables, for instance if you have an array of ints, change it to an array of atomic

std::atomic<int> arr[4];

6 Comments

My array looks more like this std::array<MyNonAtomicStruct, 4>. I think that will make a difference. I am not sure yet as to which is the best way to sync the data, but maybe if a mutex will do, I should just do that.
Well, std::array is much better then c style arrays, but it would work the same. The solution with atomic is good because it would work without locking, a little bit like volatile in java.
No, what I mean is that I cannot have a std::atomic<NonAtomicDataStructure>. Is that accurate?
I see now what you mean, if it's specific fields in the non atomic struct, you could maybe just make those atomic. Otherwise if mutex works for you, perhaps it's better (and more readable)
I am gonna go for a mutex and expose a function through a class that receives a lambda and operates on the structure. I think it will be easier and the array holds little data. That should do it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.