| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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 class PrintInOrderManualResetEvent : IPrintInOrder |
| | 16 | | { |
| 3 | 17 | | private readonly ManualResetEvent _firstPrint = new(false); |
| 3 | 18 | | private readonly ManualResetEvent _secondPrint = new(false); |
| | 19 | |
|
| | 20 | | public void First(Action printFirst) |
| 3 | 21 | | { |
| 3 | 22 | | printFirst.Invoke(); |
| | 23 | |
|
| 3 | 24 | | _firstPrint.Set(); |
| 3 | 25 | | } |
| | 26 | |
|
| | 27 | | public void Second(Action printSecond) |
| 3 | 28 | | { |
| 3 | 29 | | _firstPrint.WaitOne(); |
| | 30 | |
|
| 3 | 31 | | printSecond.Invoke(); |
| | 32 | |
|
| 3 | 33 | | _secondPrint.Set(); |
| 3 | 34 | | } |
| | 35 | |
|
| | 36 | | public void Third(Action printThird) |
| 3 | 37 | | { |
| 3 | 38 | | _secondPrint.WaitOne(); |
| | 39 | |
|
| 3 | 40 | | printThird.Invoke(); |
| 3 | 41 | | } |
| | 42 | | } |