| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Concurrency.PrintInOrder; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class PrintInOrderSemaphoreSlim : IPrintInOrder |
| | | 16 | | { |
| | 3 | 17 | | private readonly SemaphoreSlim _firstPrint = new(0, 1); |
| | 3 | 18 | | private readonly SemaphoreSlim _secondPrint = new(0, 1); |
| | | 19 | | |
| | | 20 | | public void First(Action printFirst) |
| | 3 | 21 | | { |
| | 3 | 22 | | printFirst.Invoke(); |
| | | 23 | | |
| | 3 | 24 | | _firstPrint.Release(); |
| | 3 | 25 | | } |
| | | 26 | | |
| | | 27 | | public void Second(Action printSecond) |
| | 3 | 28 | | { |
| | 3 | 29 | | _firstPrint.Wait(); |
| | | 30 | | |
| | 3 | 31 | | printSecond.Invoke(); |
| | | 32 | | |
| | 3 | 33 | | _secondPrint.Release(); |
| | 3 | 34 | | } |
| | | 35 | | |
| | | 36 | | public void Third(Action printThird) |
| | 3 | 37 | | { |
| | 3 | 38 | | _secondPrint.Wait(); |
| | | 39 | | |
| | 3 | 40 | | printThird.Invoke(); |
| | 3 | 41 | | } |
| | | 42 | | } |