C# Using Interlocked and SpinWait faster than 100.00% less than 85.61% memory
Runtime: 44 ms\nMemory Usage: 16.7 MB\n\n\nusing System.Threading;\npublic class FooBar\n{\n    private readonly int n;\n    volatile int foo = 0;\n    volatile
Runtime: 44 ms\nMemory Usage: 16.7 MB\n\n```\nusing System.Threading;\npublic class FooBar\n{\n    private readonly int n;\n    volatile int foo = 0;\n    volatile int bar = 1;\n    SpinWait spinWait = new SpinWait();\n\n    public FooBar(int n)\n    {\n        this.n = n;\n    }\n\n    public void Foo(Action printFoo)\n    {\n        for (int i = 0; i < n; i++)\n        {\n            while (Interlocked.CompareExchange(ref foo, 1, 0) != 0) {spinWait.SpinOnce();}\n            printFoo();\n            Interlocked.Exchange(ref bar, 0);\n        }\n    }\n\n    public void Bar(Action printBar)\n    {\n        for (int i = 0; i < n; i++)\n        {\n            while (Interlocked.CompareExchange(ref bar, 1, 0) != 0) {spinWait.SpinOnce();}\n            printBar();\n            Interlocked.Exchange(ref foo, 0);\n        }\n    }\n}\n```\n
[]