Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


NativeStream

A set of untyped, append-only buffers. Allows for concurrent reading and concurrent writing without synchronization.
Read time 2 minutesLast updated 12 days ago

Definition

public struct NativeStream : INativeDisposable, IDisposable

Remarks

As long as each individual buffer is written in one thread and read in one thread, multiple threads can read and write the stream concurrently, "e.g." while thread "A" reads from buffer "X" of a stream, thread "B" can read from buffer "Y" of the same stream.
Each buffer is stored as a chain of blocks. When a write exceeds a buffer's current capacity, another block is allocated and added to the end of the chain. Effectively, expanding the buffer never requires copying the existing data (unlike with NativeList<T>, for example).
All writing to a stream should be completed before the stream is first read. Do not write to a stream after the first read. Violating these rules won't "necessarily" cause any problems, but they are the intended usage pattern.
Writing is done with NativeStream.Writer, and reading is done with NativeStream.Reader. An individual reader or writer cannot be used concurrently across threads: each thread must use its own.
The data written to an individual buffer can be heterogeneous in type, and the data written to different buffers of a stream can be entirely different in type, number, and order. Just make sure that the code reading from a particular buffer knows what to expect to read from it.

Constructors

Constructor

Description

NativeStream(System.Int32,Unity.Collections.AllocatorManager.AllocatorHandle)Initializes and returns an instance of NativeStream.

Properties

Property

Description

ForEachCountThe number of buffers in this stream.
IsCreatedWhether this stream has been allocated (and not yet deallocated).

Methods

Method

Description

AsReaderReturns a reader of this stream.
AsWriterReturns a writer of this stream.
CountReturns the total number of items in the buffers of this stream.
DisposeReleases all resources (memory and safety handles).
IsEmptyReturns true if this stream is empty.
ToNativeArrayReturns a new NativeArray copy of this stream's data.

Static Methods

Method

Description

ScheduleConstructCreates and schedules a job to allocate a new stream.

Structs

Struct

Description

NativeStream.ReaderReads data from a buffer of a NativeStream.
NativeStream.WriterWrites data into a buffer of a NativeStream.