The Erlang process is similar to the executed method in parallel, but the erlang variable can only be bound once, so it is thread safe, which is not in C #.
so you need two things in C #, thread safety and concurrency.
C # has System.Threading.Task, you can run many tasks in vm. C # vm will schedule this task in different workflows.
But the task is not thread safe, you need to create a class called "Actor" and put the state in Actor.
The actor has System.Threading.SynchronizationContext and many async methods, for example.
class Actor { public SynchronizationContext _context; private int value1; private Dictionary<> xxx; private List<> xxx; public async Task Method1() { await _context; doSomething(); } }
When other Actors call the asynchronization method in this Actor, it will create a task and the task will be scheduled using vm.
You also need to implement the expected and thread safe SynchronizationContext.
it is a safe stream.
public class ActorSynchronizationContext : SynchronizationContext { private readonly SynchronizationContext _subContext; private readonly ConcurrentQueue<Action> _pending = new ConcurrentQueue<Action>(); private int _pendingCount; public ActorSynchronizationContext(SynchronizationContext context = null) { this._subContext = context ?? new SynchronizationContext(); } public override void Post(SendOrPostCallback d, object state) { if (d == null) { throw new ArgumentNullException("SendOrPostCallback"); } _pending.Enqueue(() => d(state)); if (Interlocked.Increment(ref _pendingCount) == 1) { try { _subContext.Post(Consume, null); } catch (Exception exp) { LogHelper.LogUnhandleException(exp.ToString()); } } } private void Consume(object state) { var surroundContext = Current; SetSynchronizationContext(this); do { Action a; _pending.TryDequeue(out a); try { a.Invoke(); } catch (Exception exp) {
make SynchroniztionContext expected
public static class SynchroniztionContextExtensions { public static SynchronizationContextAwaiter GetAwaiter (this SynchronizationContext context) { if(context == null) throw new ArgumentNullException("context"); return new SynchronizationContextAwaiter(context); } }
Awaiter for SynchronizationContext
public sealed class SynchronizationContextAwaiter : INotifyCompletion { private readonly SynchronizationContext _context; public SynchronizationContextAwaiter(SynchronizationContext context) { if(context == null ) throw new ArgumentNullException("context"); _context = context; } public bool IsCompleted { get {
then you get an Actor class with task security and thread, which are like a process in erlang.