Skip to content

Interlocked.CompareExchange missing for uint, ulong and (if possible) general structs. #8153

@redknightlois

Description

@redknightlois

I recently needed to use Interlocked.CompareExchange inside a function with the following form:

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private bool TryClaimSlot(ref uint entryKey, int key)
        {
            var entryKeyValue = entryKey;
            //zero keys are claimed via hash
            if (entryKeyValue == 0 & key != 0)
            {
                entryKeyValue = Interlocked.CompareExchange(ref entryKey, key, 0);
                if (entryKeyValue == 0)
                {
                    // claimed a new slot
                    this.allocatedSlotCount.Increment();
                    return true;
                }
            }

            return key == entryKeyValue;
        }

problem is that it can´t be done because the Interlocked.CompareExchange<T> requires it to be a class.

Also if anyone knows a workaround for it, I would appreciate it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.ThreadingenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions