예제 #1
0
        public async void Test2()
        {
            var cv        = new ConditionVariable();
            var data      = (string?)null;
            var ready     = false;
            var processed = false;

            // Main
            {
                var worker = new Thread(new ThreadStart(WorkerThread)).Run();

                data = "Example data";

                // send data to the worker thread
                ready = true;
                cv.NotifyOne();

                // wait for the worker
                await cv.WaitAsync(() => processed);

                // "Back in main(), data = " << data << '\n';
                Assert.Equal(data, "Changed data");

                worker.Join();
            }

            async void WorkerThread()
            {
                // Wait until main() sends data
                await cv.WaitAsync(() => ready);

                // after the wait, we own the lock.
                data = "Changed data";

                // Send data back to main()
                processed = true;

                // Should be notifying outside of the lock but C# Monitor.Pulse is
                // not quite the same as std::condition_variable.notifiy_all()
                cv.NotifyOne();
            }
        }