ConcurrentQueue<T>队列是一个高效的线程安全的队列,是.Net Framework 4.0,System.Collections.Concurrent命名空间下的一个数据结构。

ConcurrentQueue<T>数据结构

下图是ConcurrentQueue<T>数据结构的示意图:

ConcurrentQueue<T>队列由若干Segment动态构成,每个Segment是一块连续的内存Buffer,大小固定为SEGMENT_SIZE。

ConcurrentQueue<T>私有成员变量

ConcurrentQueue<T>类有三个私有成员变量:

Segment* volatile m_head;

Segment* volatile m_tail;

Segment* volatile m_base;

m_head指向第一个segment,m_tail指向最后一个segment。这两个指针指向的对象,随着入队列和出队列操作而不断变化。

m_base指针固定指向ConcurrentQueue<T>实例化的第一个Segment,在析构ConcurrentQueue<T>对象时使用。

ConcurrentQueue<T>成员方法

void Enqueue(T item)
1
2
3
4
5
6
7
8
void Enqueue(T item)
{
    DNetSpinWait wait;
    while (!m_tail->TryAppend(item, &m_tail))
    {
        wait.SpinOnce();
    }
}
1
从m_tail指向的segment中,加入item的值,直到成功加入,函数返回。

该函数会在分配了新的segment后,更新m_tail指针。

bool TryDequeue(T* result)
1
2
3
4
5
6
7
8
9
10
11
12
13
bool TryDequeue(T* result)
{
 
    while (!IsEmpty())
    {
        if (m_head->TryRemove(result, &m_head))
        {
            return true;
        }  
    }
    result = NULL;
    return false;
}

如果当前队列为空,返回false,否则返回队列的第一个元素。

bool TryPeek(T* result)
1
2
3
4
5
6
7
8
9
10
11
12
bool TryPeek(T* result)
{
    while (!IsEmpty())
    {
        if (m_head->TryPeek(result))
        {
            return true;
        }
    }
    result = NULL;
    return false;
}

跟TryDequeue()方法相似。

int Count()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Count()
{
    Segment* segment;
    Segment* segment2;
    int num;
    int num2;
    GetHeadTailPositions(&segment, &segment2, &num, &num2);
    if (segment == segment2)
    {
        return ((num2 - num) + 1);
    }
    int num3 = SEGMENT_SIZE - num;
    num3 += SEGMENT_SIZE * (((int) (segment2->GetIndex() - segment->GetIndex())) - 1);
    return (num3 + (num2 + 1));
}

通过得到当前首尾的segment指针,以及首指针的m_low索引,以及尾指针的m_high索引,计算当前队列中元素的个数。

该方法用到了GetHeadTailPositions方法。

bool IsEmpty()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool IsEmpty()
{
    Segment* head = m_head;
    if (head->IsEmpty())
    {
        if (head->GetNext() == NULL)
        {
            return true;
        }
        DNetSpinWait wait;
        while (head->IsEmpty())
        {
            if (head->GetNext() == NULL)
            {
                return true;
            }
            wait.SpinOnce();
            head = m_head;
        }
    }
    return false;
}

判定当前队列为空有两个条件,第一,m_head指向的segment为空;第二,m_head->GetNext()也为空,即m_head和m_tail指向同一个segment。

void Reset()
1
2
3
4
5
void Reset()
{
    DeleteNodes();
    m_base = m_head = m_tail = new Segment(0);
}

重置ConcurrentQueue<T>对象,删除已经分配了的segment,并重新更新成员变量的值。

void GetHeadTailPositions(Segment** head, Segment** tail, int* headLow, int* tailHigh)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void GetHeadTailPositions(Segment** head, Segment** tail, int* headLow, int* tailHigh)
{
    *head = m_head;
    *tail = m_tail;
    *headLow = (*head)->GetLow();
    *tailHigh = (*tail)->GetHigh();
    DNetSpinWait wait;
    while ((((*head != m_head) || (*tail != m_tail)) ||
        ((*headLow != (*head)->GetLow()) || (*tailHigh != (*tail)->GetHigh()))) ||
        ((*head)->GetIndex() > (*tail)->GetIndex()))
    {
        wait.SpinOnce();
        *head = m_head;
        *tail = m_tail;
        *headLow = (*head)->GetLow();
        *tailHigh = (*tail)->GetHigh();
    }
}

该函数就是将队列当前的m_head, m_tail指针以及m_head的m_low索引,m_tail的m_high索引取出来,放到线程栈上。并且在取出这些值后,再判断这些值是否合法。

c#高效的线程安全队列ConcurrentQueue<T>(上)的更多相关文章

  1. c# 高效的线程安全队列ConcurrentQueue

    c#高效的线程安全队列ConcurrentQueue<T>(上) c# 高效的线程安全队列ConcurrentQueue(下) Segment类 c#高效的线程安全队列Concurrent ...

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

    Segment成员变量 long long m_index; 记录该segment的索引号. int* volatile m_state; 状态数组,标识所对应的元素节点的状态,默认值为0,如果该元素 ...

  3. C#-----线程安全的ConcurrentQueue<T>队列

     ConcurrentQueue<T>队列是一个高效的线程安全的队列,是.Net Framework 4.0,System.Collections.Concurrent命名空间下的一个数据 ...

  4. 线程安全的ConcurrentQueue<T>队列

    队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. ConcurrentQueue< ...

  5. [一起读源码]走进C#并发队列ConcurrentQueue的内部世界

    决定从这篇文章开始,开一个读源码系列,不限制平台语言或工具,任何自己感兴趣的都会写.前几天碰到一个小问题又读了一遍ConcurrentQueue的源码,那就拿C#中比较常用的并发队列Concurren ...

  6. 线程池 队列 synchronized

    线程池 BlockingQueue synchronized volatile 本章从线程池到阻塞队列BlockingQueue.从BlockingQueue到synchronized 和 volat ...

  7. 生产消费模式:多线程读写队列ConcurrentQueue

    需求:现需要将多个数据源的数据导入到目标数据库,这是一个经典的生产消费应用的例子. 直接上代码,看下实现: // 初始化列队缓冲区 队列大小为100 IDataCollection<List&l ...

  8. C#数据结构-线程安全队列

    什么是线程安全? 答:线程安全是多线程编程时的计算机程序代码中的一个概念.在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意 ...

  9. Linux多线程系列-2-条件变量的使用(线程安全队列的实现)

    多线程情况下,往往需要使用互斥变量来实现线程间的同步,实现资源正确共享. linux下使用如下变量和函数 //条件变量 pthread_cond_t int pthread_cond_init (pt ...

随机推荐

  1. Hadoop学习笔记3---安装并运行Hadoop

    本文环境是在Ubuntu10.04环境下运行的. 在Linux上安装Hadoop之前,首先安装两个程序: 1.JDK1.6(或更高版本).Hadoop是用Java编写的程序,Hadoop编译及MapR ...

  2. Web Api 2 怎么支持 Session

    Add protected void Application_PostAuthorizeRequest() { System.Web.HttpContext.Current.SetSessionSta ...

  3. JXSE and Equinox Tutorial, Part 1

    http://java.dzone.com/articles/jxse-and-equinox-tutorial-part —————————————————————————————————————— ...

  4. hdu 1260 Tickets

    http://acm.hdu.edu.cn/showproblem.php?pid=1260 题目大意:n个人买票,每个人买票都花费时间,相邻的两个人可以一起买票以节约时间: 所以一个人可以自己买票也 ...

  5. JQuery中attr ,html,text,val,的一些用法

    attr:主要获取元素内部的属性,返回 的是属性值 html:返回当前元素(不包括他自己本身的标签,但是可以返回他自己的)的标签加上内容.仅限于返回第一个. text:和 .html() 方法不同, ...

  6. Codeforces 706 C. Hard problem (dp)

    题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满 ...

  7. HDU 4493 Tutor (控制精度)

    题意:给定12个数,求平均数. 析:这个题就是精度控制问题,如果控制精度,最好的办法就是用整型了. 代码如下: #include <cstdio> #include <string& ...

  8. iOS7 各种问题解决

    1 UITableView 行分割线不到头,短线问题 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { ...

  9. jquery操作select 的value和text值

    获取select 的text值: $('#testSelect option:selected').text(); 获取select 的value值: $('#testSelect').val(); ...

  10. IE6 背景透明

    IE6 背景透明 第 1 种方法:定义一个样式,给某个div应用这个样式后,div的透明png背景图片自动透明了.(注意两处图片的路径写法不一样,本例中,icon_home.png图片与html文件在 ...