基于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_async_mutex.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <conio.h>
  6. #include <thread>
  7. #include <deque>
  8. #include "librf.h"
  9. using namespace resumef;
  10. using namespace std::chrono;
  11. static mutex_t g_lock;
  12. static intptr_t g_counter = 0;
  13. static const size_t N = 10;
  14. //🔒-50ms-🔒🗝🗝-150ms-|
  15. //-------------.........
  16. static future_t<> test_mutex_pop(size_t idx)
  17. {
  18. for (size_t i = 0; i < N / 2; ++i)
  19. {
  20. {
  21. scoped_unlock_t _locker = co_await g_lock.lock(); //_locker析构后,会调用对应的unlock()函数。
  22. --g_counter;
  23. std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  24. co_await 50ms;
  25. scoped_unlock_t _locker_2 = co_await g_lock;
  26. --g_counter;
  27. std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  28. }
  29. co_await 150ms;
  30. }
  31. }
  32. //🔒-50ms-🗝-50ms-🔒-50ms-🗝-50ms-|
  33. //---------........---------.......
  34. //方法之一
  35. static future_t<> test_mutex_push(size_t idx)
  36. {
  37. for (size_t i = 0; i < N; ++i)
  38. {
  39. {
  40. scoped_unlock_t _locker = co_await g_lock.lock();
  41. ++g_counter;
  42. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  43. co_await 50ms;
  44. }
  45. co_await 50ms;
  46. }
  47. }
  48. static future_t<> test_mutex_try_push(size_t idx)
  49. {
  50. for (size_t i = 0; i < N; ++i)
  51. {
  52. {
  53. while (!co_await g_lock.try_lock())
  54. co_await yield();
  55. ++g_counter;
  56. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  57. co_await 50ms;
  58. co_await g_lock.unlock();
  59. }
  60. co_await 50ms;
  61. }
  62. }
  63. static future_t<> test_mutex_timeout_push(size_t idx)
  64. {
  65. for (size_t i = 0; i < N; ++i)
  66. {
  67. {
  68. while (!co_await g_lock.try_lock_for(10ms))
  69. co_await yield();
  70. ++g_counter;
  71. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  72. co_await 50ms;
  73. co_await g_lock.unlock();
  74. }
  75. co_await 50ms;
  76. }
  77. }
  78. //🔒-50ms-🗝-50ms-🔒-50ms-🗝-50ms-|
  79. //---------........---------.......
  80. static std::thread test_mutex_async_push(size_t idx)
  81. {
  82. return std::thread([=]
  83. {
  84. char provide_unique_address = 0;
  85. for (size_t i = 0; i < N; ++i)
  86. {
  87. if (g_lock.try_lock_for(500ms, &provide_unique_address))
  88. {
  89. scoped_unlock_t _locker(std::adopt_lock, &provide_unique_address, g_lock);
  90. ++g_counter;
  91. std::cout << "push:" << g_counter << " on " << idx << std::endl;
  92. std::this_thread::sleep_for(50ms);
  93. //g_lock.unlock(&provide_unique_address);
  94. }
  95. std::this_thread::sleep_for(50ms);
  96. }
  97. });
  98. }
  99. static void resumable_mutex_synch()
  100. {
  101. go test_mutex_timeout_push(0);
  102. go test_mutex_pop(1);
  103. this_scheduler()->run_until_notask();
  104. std::cout << "result:" << g_counter << std::endl;
  105. }
  106. static void resumable_mutex_async()
  107. {
  108. auto th = test_mutex_async_push(0);
  109. std::this_thread::sleep_for(25ms);
  110. go test_mutex_pop(1);
  111. this_scheduler()->run_until_notask();
  112. th.join();
  113. std::cout << "result:" << g_counter << std::endl;
  114. }
  115. static future_t<> resumable_mutex_range_push(size_t idx, mutex_t a, mutex_t b, mutex_t c)
  116. {
  117. for (int i = 0; i < 100000; ++i)
  118. {
  119. scoped_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
  120. assert(a.is_locked());
  121. assert(b.is_locked());
  122. assert(c.is_locked());
  123. ++g_counter;
  124. //std::cout << "push:" << g_counter << " on " << idx << std::endl;
  125. //co_await 5ms;
  126. }
  127. }
  128. static future_t<> resumable_mutex_range_pop(size_t idx, mutex_t a, mutex_t b, mutex_t c)
  129. {
  130. for (int i = 0; i < 100000; ++i)
  131. {
  132. scoped_unlock_t __lockers = co_await mutex_t::lock(a, b, c);
  133. assert(a.is_locked());
  134. assert(b.is_locked());
  135. assert(c.is_locked());
  136. --g_counter;
  137. //std::cout << "pop :" << g_counter << " on " << idx << std::endl;
  138. //co_await 5ms;
  139. }
  140. }
  141. static void resumable_mutex_lock_range()
  142. {
  143. mutex_t mtxA, mtxB, mtxC;
  144. //不同的线程里加锁也需要是线程安全的
  145. std::thread push_th([&]
  146. {
  147. local_scheduler __ls__;
  148. go resumable_mutex_range_push(10, mtxA, mtxB, mtxC);
  149. go resumable_mutex_range_push(11, mtxA, mtxC, mtxB);
  150. this_scheduler()->run_until_notask();
  151. });
  152. go resumable_mutex_range_pop(12, mtxC, mtxB, mtxA);
  153. go resumable_mutex_range_pop(13, mtxB, mtxA, mtxC);
  154. this_scheduler()->run_until_notask();
  155. push_th.join();
  156. std::cout << "result:" << g_counter << std::endl;
  157. }
  158. void resumable_main_mutex()
  159. {
  160. resumable_mutex_synch();
  161. std::cout << std::endl;
  162. resumable_mutex_async();
  163. std::cout << std::endl;
  164. resumable_mutex_lock_range();
  165. }