Question

After several months of evaluating and reevaluating and planing different data structures and web/application servers I'm now at a point where I need to bang my head around with implementation details. The (at the moment theoretical) question I'm facing is this:

Say I'm using GWANs KV store to store C structs for Users and the like (works fine, tested), how should I go about removing these objects from KV, and later on from memory, without encountering a race condition?

This is what I'm at at the moment:

Thread A:

  • grab other objects referencing the one to be deleted
  • set references to NULL
  • delete object

Thread B:

  • try to get object -> kv could return object, as it's not yet deleted
  • try to do something with the object -> could already be deleted here, so I would access already freed memory?

or something else which could happen:

Thread B:

  • get thing referencing object
  • follow reference -> object might not be deleted here
  • do something with reference -> object might be deleted here -> problem

or

Thread B:

  • got some other object which could reference the to be deleted object
  • grab object which isn't yet deleted
  • set reference to object -> object might be deleted here -> problem

Is there a way to avoid those kind of conditions, except for using locks? I've found a latitude of documents describing algorithms dealing with different producer/consumer situations, hashtables, ... with even sometimes wait free implementations (I haven't yet found a good example to show me the difference between lock-free and wait-free, though I get it conceptually), but I haven't been able to figure out how to deal with these kind of things.

Am I overthinking this, or is there maybe an easy way to avoid all these situations? I'm free to change the data- and -storage layout in any way I want, and I can use processor specific instructions freely (e.g. CAS)

Thanks in advance

Was it helpful?

Solution

Several questions there:

deleting a GWAN KV stored struct

When removing a KV from a persistence pointer or freeing the KV, you have to make sure that nobody is dereferencing freed data.

This is application dependent. You can introduce some tolerance by using G-WAN memory pools which will make data survive a KV deletion as long as the memory is not overwrited (or the pool freed).

deleting a GWAN KV key-value pair

G-WAN's KV store does the bookkeeping (using atomic intrinsics) to protect values fetched by threads and unprotects them after the request has been processed.

If you need to keep data for a longer time, make a copy.

Other storage tools, like in-memory SQLite use locks. In this case, lock granularity is very important.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top