MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_scq_sync_multi_producer

Function test_scq_sync_multi_producer

src/core/test/utils/thread.cpp:25–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23
24template <int producer_sleep, int consumer_sleep>
25void test_scq_sync_multi_producer() {
26 size_t nr_worker_call = 0;
27 SCQueueSynchronizer sync(0);
28 auto worker = [&]() {
29 RNGxorshf rng{next_rand_seed()};
30 while (auto nr = sync.consumer_fetch(1)) {
31 nr_worker_call += nr;
32 ASSERT_EQ(1u, nr);
33 if (consumer_sleep) {
34 std::this_thread::sleep_for(
35 std::chrono::microseconds(rng() % consumer_sleep));
36 }
37 sync.consumer_commit(nr);
38 }
39 };
40 sync.start_worker(std::thread{worker});
41
42 constexpr size_t N = 500, M = 8;
43 std::atomic_size_t nr_worker_started{0};
44 auto producer_impl = [&]() {
45 RNGxorshf rng{next_rand_seed()};
46 ++nr_worker_started;
47 while (nr_worker_started.load() != M)
48 ;
49 for (size_t i = 0; i < N; ++i) {
50 if (producer_sleep) {
51 std::this_thread::sleep_for(
52 std::chrono::microseconds(rng() % producer_sleep));
53 }
54 sync.producer_add();
55 if (i % 4 == 0)
56 sync.producer_wait();
57 }
58 };
59 std::vector<std::thread> producer_threads;
60 for (size_t i = 0; i < M; ++i) {
61 producer_threads.emplace_back(producer_impl);
62 }
63 for (auto&& i : producer_threads)
64 i.join();
65 sync.producer_wait();
66 ASSERT_EQ(N * M, nr_worker_call);
67}
68} // namespace
69
70TEST(TestAsyncQueue, Synchronizer) {

Callers

nothing calls this directly

Calls 8

consumer_fetchMethod · 0.80
consumer_commitMethod · 0.80
producer_addMethod · 0.80
producer_waitMethod · 0.80
emplace_backMethod · 0.80
joinMethod · 0.80
start_workerMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected