Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


UnsafeStream

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

Definition

public struct UnsafeStream : 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, for example, with NativeList<T>).
All writing to a stream should be completed before the stream is first read. Do not write to a stream after the first read.
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

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

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).
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

UnsafeStream.ReaderReads data from a buffer of an UnsafeStream.
UnsafeStream.ReaderStateState of a UnsafeStream.Reader.
UnsafeStream.WriterWrites data into a buffer of an UnsafeStream.