Created
March 31, 2017 13:17
-
-
Save ossiv/a98b518c64b807e76a23d87b756ad2b9 to your computer and use it in GitHub Desktop.
Redux concurrency problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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