Skip to content
This repository was archived by the owner on Jul 28, 2020. It is now read-only.
This repository was archived by the owner on Jul 28, 2020. It is now read-only.

ConcurrentBatchQueue 线程安全问题 #96

@scvegetable

Description

@scvegetable

读源码发现ConcurrentBatchQueue的TryDequeue可能有线程安全问题,经过测试发现问题确实存在。我的测试方法是开两个线程写数据,一个线程写1到1000,另一个线程写1001到2000,然后再开N个线程读数据,读够2000个数据后,把读出来的数据排序然后对比是否为1到2000的连续数字,如果是则测试成功,否则失败。测试结果与预期一致,单线程读多线程写没有线程安全问题,多线程读则会读到重复的数字,修改源码判断是否有其他线程正在 读之后,问题消除。

    public bool TryDequeue(IList<T> outputItems)
    {
        var entity = m_Entity as Entity;
        int count = entity.Count;

        if (count <= 0)
            return false;
        ....
    }

修改为

    public bool TryDequeue(IList<T> outputItems)
    {
        var entity = m_Entity as Entity;
        if (entity == m_BackEntity)
            return false;
        int count = entity.Count;

        if (count <= 0)
            return false;
        ....
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions