Skip to content

Instantly share code, notes, and snippets.

@ossiv
Created March 31, 2017 13:17
Show Gist options
  • Save ossiv/a98b518c64b807e76a23d87b756ad2b9 to your computer and use it in GitHub Desktop.
Save ossiv/a98b518c64b807e76a23d87b756ad2b9 to your computer and use it in GitHub Desktop.
Redux concurrency problem
using System;
using Redux;
using System.Threading;
using System.Diagnostics;
namespace ReduxRaceCondition
{
class MainClass
{
public class AddAction : IAction
{
public int Value { get; }
public AddAction(int value)
{
Value = value;
}
}
public static class Reducers
{
public static int MainReducer(int state, IAction action)
{
var act = action as AddAction;
return state += (act.Value);
}
}
public static void Main(string[] args)
{
var store = new Store<int>(Reducers.MainReducer, 0);
int previous = -1;
var thread1 = new Thread(() =>
{
for (int i = 0; i < 50000; ++i)
{
store.Dispatch(new AddAction(1));
Thread.Sleep(3);
}
});
var thread2 = new Thread(() =>
{
for (int i = 0; i < 50000; ++i)
{
store.Dispatch(new AddAction(1));
Thread.Sleep(3);
}
});
store.Subscribe(s =>
{
if (s < previous)
{
Debugger.Break();
}
previous = s;
Console.WriteLine(s);
});
thread2.Start();
thread1.Start();
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment