Share static data between C# and Burst
Read time 1 minuteLast updated 13 days ago
Burst has basic support for accessing static readonly data. However, if you want to share static mutable data between C# and HPC#, use the struct.
SharedStatic<T>The following example shows accessing an static field that both C# and HPC# can change:
intpublic abstract class MutableStaticTest { public static readonly SharedStatic<int> IntField = SharedStatic<int>.GetOrCreate<MutableStaticTest, IntFieldKey>(); // Define a Key type to identify IntField private class IntFieldKey {} }
C# and HPC# can then access this:
// Write to a shared static MutableStaticTest.IntField.Data = 5; // Read from a shared static var value = 1 + MutableStaticTest.IntField.Data;
When you use , be aware of the following:
SharedStatic<T>- The in
Tdefines the data type.SharedStatic<T> - To identify a static field, provide a context for it. To do this, create a key for both the containing type (for example, in the example above), identify the field (for example,
MutableStaticTestclass in the example above) and pass these classes as generic arguments ofIntFieldKey.SharedStatic<int>.GetOrCreate<MutableStaticTest, IntFieldKey>() - Always initialize the shared static field in C# from a static constructor before accessing it from HPC#. If you don't initialize the data before accessing it, it might lead to an undefined initialization state.