user352951
user352951

Reputation: 281

how to check for an array update

I have an array of some data type (int, string, or even user defined class objects) and several threads in my program. I want to be able to find if the array has been updated after it was initialized with some values. One idea is to associate the hash of values in the array and whenever I want to check if the array has been updated, recalculate the hash. Is there any other way of doing it in c++ ? or can we check if the range of memory address has been updated since we last checked or not?

Thanks

Upvotes: 0

Views: 606

Answers (2)

Steve Townsend
Steve Townsend

Reputation: 54158

You could encapsulate the array in a class template that provides a facade for whatever read and write ops you need, and also marks the array dirty when it's updated - reset the dirty flag on each inspection.

Not sure how you make this reliable across multiple reader/writer threads but I guess it depends on access pattern and the exact semantics you want.

[Hashing won't work because hashes can always collide.]

Upvotes: 3

James McNellis
James McNellis

Reputation: 355079

The hash approach only works if you don't care about the case where an object is modified but then reset back to its initial value. In this case, the object was "updated," but there's no way to tell using a hash.

Another approach is to use an update counter: keep an integer alongside the object and increment it every time you update.

Upvotes: 3

Related Questions