Thread-safe way of in-/decrementing a pointer in C# -
the fantastic interlocked class provides overload of increment:
interlocked.increment(ref int loc); this looks close looking for. only, have not variable itself, pointer it. cannot use ref need overload such:
// not exist: interlocked.increment(int* loc); is there workaround? other way efficiently , thread safe increment value via address in c#?
so internal static extern increment(ref int* loc); enough; corresponding native function looks writeable:
int *increment(int **loc) { return (int*)interlockedadd((int *)loc, sizeof(int)); } but can't use it. resulting code like:
int *locus = interlocked.increment(loc); if (locus < base + length) { // locus } but undefined. if loc gets incremented many times overflow , locus ends pointing @ low address. (base might right @ top of user memory ...).
on other hand if have pointer integer want increment; p/invoke call interlockedincrement already. oops; not on 64 bit; have build tiny c dll pick intrinsic.
Comments
Post a Comment