コード例 #1
0
    public void Enqueue()
    {
        const int queueSize   = 100 * 1024;
        var       queue       = new NativeQueue <int>(Allocator.TempJob);
        var       writeStatus = new NativeArray <int>(queueSize, Allocator.TempJob);

        var enqueueJob = new ConcurrentEnqueue();

        enqueueJob.queue  = queue;
        enqueueJob.result = writeStatus;

        enqueueJob.Schedule(queueSize, 1).Complete();

        Assert.AreEqual(queueSize, queue.Count, "Job enqueued the wrong number of values");
        var allValues = new HashSet <int>();

        for (int i = 0; i < queueSize; ++i)
        {
            Assert.AreEqual(1, writeStatus[i], "Job failed to enqueue value");
            int enqueued = queue.Dequeue();
            Assert.IsTrue(enqueued >= 0 && enqueued < queueSize, "Job enqueued invalid value");
            Assert.IsTrue(allValues.Add(enqueued), "Job enqueued same value multiple times");
        }

        queue.Dispose();
        writeStatus.Dispose();
    }
コード例 #2
0
    public void NativeQueue_ParallelWriter()
    {
        const int queueSize   = 100 * 1024;
        var       queue       = new NativeQueue <int>(Allocator.TempJob);
        var       writeStatus = new NativeArray <int>(queueSize, Allocator.TempJob);

        var jobHandle = new ConcurrentEnqueue()
        {
            queue      = queue.AsParallelWriter(),
            result     = writeStatus,
            StartIndex = 0,
        }.Schedule(queueSize / 2, 1);

        jobHandle = new ConcurrentEnqueue()
        {
            queue      = queue.AsParallelWriter(),
            result     = writeStatus,
            StartIndex = queueSize / 2,
        }.Schedule(queueSize / 2, 1, jobHandle);

        jobHandle.Complete();

        Assert.AreEqual(queueSize, queue.Count, "Job enqueued the wrong number of values");
        var allValues = new NativeHashSet <int>(queueSize, Allocator.Persistent);

        for (int i = 0; i < queueSize; ++i)
        {
            Assert.AreEqual(1, writeStatus[i], "Job failed to enqueue value");
            int enqueued = queue.Dequeue();
            Assert.IsTrue(enqueued >= 0 && enqueued < queueSize, "Job enqueued invalid value");
            Assert.IsTrue(allValues.Add(enqueued), "Job enqueued same value multiple times");
        }

        var disposeJob = queue.Dispose(jobHandle);

        disposeJob.Complete();

        writeStatus.Dispose();
        allValues.Dispose();
    }