title: Thread Safety sidebar_position: 10 id: thread_safety license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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); });
ThreadSafeFory.Register(...) stores registrations centrally.ThreadSafeFory implements IDisposable and should be disposed when no longer needed.
using ThreadSafeFory fory = Fory.Builder().BuildThreadSafe();