Skip to main content
Version: dev

Thread Safety

Apache Fory™ C# provides two runtime forms with different threading guarantees.

Fory (Single-Threaded Runtime)

Fory is optimized for single-threaded reuse and must not be used concurrently by multiple threads.

Fory fory = Fory.Builder().Build();

Use one Fory instance per thread when managing thread affinity explicitly.

ThreadSafeFory (Concurrent Wrapper)

ThreadSafeFory wraps one Fory instance per thread and exposes thread-safe APIs.

using Apache.Fory;

using ThreadSafeFory fory = Fory.Builder()
.Compatible(true)
.TrackRef(true)
.BuildThreadSafe();

fory.Register<MyType>(100);

Parallel.For(0, 64, i =>
{
byte[] payload = fory.Serialize(i);
int decoded = fory.Deserialize<int>(payload);
});

Registration Behavior

  • ThreadSafeFory.Register(...) stores registrations centrally.
  • Existing per-thread runtimes are updated.
  • New threads receive all previous registrations automatically.

Disposal

ThreadSafeFory implements IDisposable and should be disposed when no longer needed.

using ThreadSafeFory fory = Fory.Builder().BuildThreadSafe();