| 1 | // Copyright 2014 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | #include <thread> |
| 6 | |
| 7 | #include "Common/Event.h" |
| 8 | |
| 9 | using Common::Event; |
| 10 | |
| 11 | TEST(Event, MultiThreaded) |
| 12 | { |
| 13 | Event has_sent, can_send; |
| 14 | int shared_obj; |
| 15 | constexpr int ITERATIONS_COUNT = 100000; |
| 16 | |
| 17 | auto sender = [&] { |
| 18 | for (int i = 0; i < ITERATIONS_COUNT; ++i) |
| 19 | { |
| 20 | can_send.Wait(); |
| 21 | shared_obj = i; |
| 22 | has_sent.Set(); |
| 23 | } |
| 24 | }; |
| 25 | |
| 26 | auto receiver = [&] { |
| 27 | for (int i = 0; i < ITERATIONS_COUNT; ++i) |
| 28 | { |
| 29 | has_sent.Wait(); |
| 30 | EXPECT_EQ(i, shared_obj); |
| 31 | can_send.Set(); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | std::thread sender_thread(sender); |
| 36 | std::thread receiver_thread(receiver); |
| 37 | |
| 38 | can_send.Set(); |
| 39 | |
| 40 | sender_thread.join(); |
| 41 | receiver_thread.join(); |
| 42 | } |