c# 高效的线程安全队列ConcurrentQueue(下) Segment类

本文详细介绍了Segment队列的成员变量及成员函数,包括其状态数组、存储指针等关键概念,解析了如何通过Grow方法创建下一个Segment,TryAppend方法添加元素,TryPeek方法获取元素,以及TryRemove方法移除元素的过程。

 

 

Segment成员变量

 

        long long m_index;


记录该segment的索引号。

        int* volatile m_state;


状态数组,标识所对应的元素节点的状态,默认值为0,如果该元素节点添加了值,则标记为1。

        T* volatile m_array;


队列元素存储空间的指针。

        Segment* volatile m_next;


指向下一个segment的指针。

        volatile long m_high;


标识在当前segment,元素最后添加的索引值,初始值为-1,如果该segment被填满了,则该值为SEGMENT_SIZE – 1。

        volatile long m_low;


标识在当前segment,元素最后取出位置的索引值,初始值为0,如果该segment一个都没有取走元素,则该值为0。如果m_low >m_high,表示该segment为空。

Segment成员函数

 

void Grow(Segment* volatile* tail)
1
2
3
4
5
6
void Grow(Segment* volatile* tail)
{
    Segment* segment = new Segment(m_index + 1);
    m_next = segment;
    *tail = m_next;
}
1
创建下一个segment,并将tail指针指向新创建的segment;
bool TryAppend(T value, Segment* volatile *  tail)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool TryAppend(T value, Segment* volatile *  tail)
{
    if (m_high >= SEGMENT_SIZE - 1)
    {
        return false;
    }
 
    int index = SEGMENT_SIZE;
 
    index = InterlockedIncrement(&m_high);
 
    if (index <= SEGMENT_SIZE - 1)
    {
        m_array[index] = value;
        m_state[index] = 1;
    }
    if (index == SEGMENT_SIZE - 1)
    {
        Grow(tail);
    }
     
    return (index <= SEGMENT_SIZE - 1);
}

往当前segment里面,增加一个元素,如果添加满了,就创建下一个segment。

bool TryPeek(T* result)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool TryPeek(T* result)
{
    int low = GetLow();
    if (low > GetHigh())
    {
        return false;
    }
 
    DNetSpinWait wait;
    while (m_state[low] == 0)
    {
        wait.SpinOnce();
    }
    *result = m_array[low];
    return true;
}

如果segment为空,返回false,否则,返回low所在位置的值。

bool TryRemove(T* result, Segment* volatile * head)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
bool TryRemove(T* result, Segment* volatile * head)
{
    DNetSpinWait wait;
    int low = GetLow();
    for (int i = GetHigh(); low <= i; i = GetHigh())
    {
        if (InterlockedCompareExchange(&m_low, low + 1, low) == low)
        {  
 
            DNetSpinWait wait2;
            while (m_state[low] == 0)
            {
                wait2.SpinOnce();
            }
            *result = m_array[low];
            if ((low + 1) >= SEGMENT_SIZE)
            {
                wait2.Reset();
                while (m_next == NULL)
                {
                    wait2.SpinOnce();
                }
                *head = m_next;
            }
            return true;
        }
        wait.SpinOnce();
        low = GetLow();
    }
    result = NULL;
    return false;
}


这是最复杂的一个方法。利用了InterlockedCompareExchange方法,该方法的解释:
LONG __cdecl InterlockedCompareExchange(
  __inout  LONG volatile* Destination,
  __in     LONG Exchange,
  __in     LONG Comparand
);
Parameters
Destination 
A pointer to the destination value. The sign is ignored.

Exchange 
The exchange value. The sign is ignored.

Comparand 
The value to compare to Destination. The sign is ignored.

Return Value
The function returns the initial value of the Destination parameter.

通过自旋来保证线程同步。

int GetHigh()



    return min(m_high, SEGMENT_SIZE - 1);
}

bool IsEmpty()


{
    return m_low > m_high;
}

int GetLow()



    return min(m_low, SEGMENT_SIZE);
}

Segment* GetNext()


{
    return m_next;
}

long long GetIndex()


{
    return m_index;
}

转载于:https://www.cnblogs.com/lvdongjie/p/5428357.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值