| 10 | #if MGB_HAVE_THREAD |
| 11 | using namespace mgb; |
| 12 | TEST(TestThreadPool, BASIC) { |
| 13 | auto thread_pool0 = std::make_shared<ThreadPool>(1u); |
| 14 | auto thread_pool1 = std::make_shared<ThreadPool>(4u); |
| 15 | ASSERT_EQ(thread_pool0->nr_threads(), static_cast<size_t>(1)); |
| 16 | ASSERT_EQ(thread_pool1->nr_threads(), static_cast<size_t>(4)); |
| 17 | |
| 18 | std::vector<int> source(100), dst0(100), dst1(100), truth(100); |
| 19 | std::atomic_size_t count0{0}, count1{0}; |
| 20 | for (int i = 0; i < 100; i++) { |
| 21 | source[i] = i; |
| 22 | dst0[i] = 0; |
| 23 | dst1[i] = 0; |
| 24 | truth[i] = i * i; |
| 25 | } |
| 26 | size_t total_task = 50; |
| 27 | auto func0 = [&](size_t index, size_t) { |
| 28 | count0++; |
| 29 | size_t sub_task = 100 / total_task; |
| 30 | for (size_t i = index * sub_task; i < (index + 1) * sub_task; i++) { |
| 31 | dst0[i] = source[i] * source[i]; |
| 32 | } |
| 33 | }; |
| 34 | auto func1 = [&](size_t index, size_t) { |
| 35 | count1++; |
| 36 | size_t sub_task = 100 / total_task; |
| 37 | for (size_t i = index * sub_task; i < (index + 1) * sub_task; i++) { |
| 38 | dst1[i] = source[i] * source[i]; |
| 39 | } |
| 40 | }; |
| 41 | thread_pool0->active(); |
| 42 | thread_pool0->add_task({func0, total_task}); |
| 43 | thread_pool0->deactive(); |
| 44 | thread_pool1->active(); |
| 45 | thread_pool1->add_task({func1, total_task}); |
| 46 | thread_pool1->deactive(); |
| 47 | ASSERT_EQ(count0, total_task); |
| 48 | ASSERT_EQ(count1, total_task); |
| 49 | for (size_t i = 0; i < 100; i++) { |
| 50 | ASSERT_EQ(dst0[i], truth[i]); |
| 51 | ASSERT_EQ(dst1[i], truth[i]); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | TEST(TestGraph, ParallelRunMultithreadMode) { |
| 56 | // check race conditions when graphs are executed on multple threads |
nothing calls this directly
no test coverage detected