基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ring_queue.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #include <cstdint>
  3. #include <thread>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <random>
  7. template<class ring_queue>
  8. bool test_ring_queue_thread(uint32_t seed, bool printResult)
  9. {
  10. ring_queue q{ 12 };
  11. const size_t N = 1000000;
  12. std::atomic<int64_t> total_push = 0, total_pop = 0;
  13. std::thread th_push[2];
  14. for (auto& th : th_push)
  15. {
  16. th = std::thread([=, &q, &total_push]
  17. {
  18. std::mt19937 rd_generator{ seed };
  19. std::uniform_int_distribution<uint32_t> distribution{ 0, 9 };
  20. for (size_t i = 0; i < N; ++i)
  21. {
  22. int value = (int)distribution(rd_generator);
  23. total_push += value;
  24. while (!q.try_push(value))
  25. std::this_thread::yield();
  26. }
  27. if (printResult)
  28. std::cout << "+";
  29. });
  30. }
  31. std::thread th_pop[2];
  32. for (auto& th : th_pop)
  33. {
  34. th = std::thread([=, &q, &total_pop]
  35. {
  36. for (size_t i = 0; i < N; ++i)
  37. {
  38. int value;
  39. while (!q.try_pop(value))
  40. std::this_thread::yield();
  41. total_pop += value;
  42. }
  43. if (printResult)
  44. std::cout << "-";
  45. });
  46. }
  47. for (auto& th : th_push)
  48. th.join();
  49. for (auto& th : th_pop)
  50. th.join();
  51. //assert(total_push.load() == total_pop.load());
  52. if (printResult)
  53. {
  54. std::cout << std::endl;
  55. std::cout << "push = " << total_push.load() << ", pop = " << total_pop.load() << std::endl;
  56. }
  57. return total_push.load() == total_pop.load();
  58. }
  59. template<class ring_queue>
  60. void test_ring_queue_simple()
  61. {
  62. ring_queue rq_zero{ 0 };
  63. assert(rq_zero.empty());
  64. assert(rq_zero.full());
  65. assert(rq_zero.size() == 0);
  66. ring_queue rq{ 2 };
  67. assert(rq.size() == 0);
  68. assert(rq.empty());
  69. assert(!rq.full());
  70. assert(rq.try_push(10));
  71. assert(rq.size() == 1);
  72. assert(!rq.empty());
  73. assert(!rq.full());
  74. assert(rq.try_push(11));
  75. assert(rq.size() == 2);
  76. assert(!rq.empty());
  77. assert(rq.full());
  78. assert(!rq.try_push(12));
  79. int value;
  80. (void)value;
  81. assert(rq.try_pop(value) && value == 10);
  82. assert(rq.size() == 1);
  83. assert(!rq.empty());
  84. assert(!rq.full());
  85. assert(rq.try_pop(value) && value == 11);
  86. assert(rq.size() == 0);
  87. assert(rq.empty());
  88. assert(!rq.full());
  89. assert(!rq.try_pop(value));
  90. assert(rq.try_push(13));
  91. assert(rq.size() == 1);
  92. assert(!rq.empty());
  93. assert(!rq.full());
  94. assert(rq.try_pop(value) && value == 13);
  95. assert(rq.size() == 0);
  96. assert(rq.empty());
  97. assert(!rq.full());
  98. }
  99. template<class ring_queue>
  100. void test_ring_queue()
  101. {
  102. using namespace std::chrono;
  103. std::random_device rd{};
  104. test_ring_queue_simple<ring_queue>();
  105. auto tp_start = system_clock::now();
  106. for (int i = 0; i < 20; ++i)
  107. {
  108. if (test_ring_queue_thread<ring_queue>(rd(), false))
  109. std::cout << ".";
  110. else
  111. std::cout << "E";
  112. }
  113. auto dt = system_clock::now() - tp_start;
  114. std::cout << std::endl;
  115. std::cout << "cost time: " << duration_cast<duration<double>>(dt).count() << "s." << std::endl;
  116. }