I'm implementing a reference-counting system in C that needs to work with multiple threads. As a result, I need a way to decrement an integral reference count and test if the result is zero with one atomic operation. I can use C11 and stdatomic.h
, but there doesn't seem to be a decrement-and-test operation.
What's the best (i.e. most portable) way of going about this? Can I use the stdatomic.h
functions to achieve this?
This is the core of the reference counting (pseudocode):
retain(object) {
++object.ref_count; // pretty easy to make this atomic
}
release(object) {
if (--object.ref_count == 0) // need this to be atomic also
free(object)
}
Aucun commentaire:
Enregistrer un commentaire