Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <future>
  3. #include <condition_variable>
  4. #include <mutex>
  5. #include <thread>
  6. #include <queue>
  7.  
  8.  
  9. // One Way Channel a.k.a. Message Queue
  10. template <typename T>
  11. class OneWayChannel {
  12. public:
  13. void push(T msg) {
  14. std::lock_guard<std::mutex> lock(mutex_);
  15. msgs_.push(std::move(msg));
  16. cond_.notify_one();
  17. }
  18.  
  19. T pop() {
  20. std::unique_lock<std::mutex> lock(mutex_);
  21. cond_.wait(lock, [this]{ return !msgs_.empty(); });
  22. T msg = std::move(msgs_.front());
  23. msgs_.pop();
  24. return msg;
  25. }
  26. private:
  27. std::mutex mutex_;
  28. std::condition_variable cond_;
  29. std::queue<T> msgs_;
  30. };
  31.  
  32. void test_channel_async() {
  33. OneWayChannel<std::string> queue;
  34. std::future<void> fut = std::async(std::launch::async, [&queue]{ queue.push("Hello from thread"); });
  35. std::cout << "waiting from thread...\n";
  36. std::cout << queue.pop() << std::endl;
  37. fut.wait();
  38. }
  39.  
  40. void test_channel_thread() {
  41. OneWayChannel<std::string> queue;
  42. std::thread th([](OneWayChannel<std::string> & queue) { queue.push("Hello from thread2"); }, std::ref(queue));
  43. std::cout << "waiting from thread...\n";
  44. std::cout << queue.pop() << std::endl;
  45. th.join();
  46. }
  47.  
  48. void func(std::promise<std::string> && pr, bool flag) {
  49. try {
  50. if (flag) {
  51. pr.set_value("Hello from thread 3");
  52. } else {
  53. throw std::runtime_error("A-a-a-ah");
  54. }
  55. } catch(...) {
  56. pr.set_exception(std::current_exception());
  57. }
  58. }
  59.  
  60. void test_promise(bool flag) {
  61. std::promise<std::string> pr;
  62. std::future<std::string> fut = pr.get_future();
  63.  
  64. std::thread th(func, std::move(pr), flag);
  65. try {
  66. std::cout << "waiting from thread...\n";
  67. std::cout << fut.get() << std::endl;
  68. } catch (const std::exception &ex) {
  69. std::cout << "caught: " << ex.what() << std::endl;
  70. }
  71. th.join();
  72. }
  73.  
  74. std::string func2(bool flag) {
  75. if (flag) {
  76. return "Hello from thread 4";
  77. } else {
  78. throw std::runtime_error("A-a-a-ah");
  79. }
  80. }
  81.  
  82. void test_async(bool flag) {
  83. std::future<std::string> fut = std::async(std::launch::async, func2, flag);
  84. try {
  85. std::cout << "waiting from thread...\n";
  86. std::cout << fut.get() << std::endl;
  87. } catch (const std::exception &ex) {
  88. std::cout << "caught: " << ex.what() << std::endl;
  89. }
  90. }
  91.  
  92. int main() {
  93. std::cout << "One Way Channel test using async\n";
  94. test_channel_async();
  95.  
  96. std::cout << "\nOne Way Channel test using threads\n";
  97. test_channel_thread();
  98.  
  99. std::cout << "\nTest promise using threads\n";
  100. test_promise(true);
  101. test_promise(false);
  102.  
  103. std::cout << "\nTest async (wrapper over threads and promise)\n";
  104. test_async(true);
  105. test_async(false);
  106. return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement