• R/O
  • SSH

提交

标签

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Comparing performance of a task queued to an existing thread vs. new thread for each task.


Commit MetaInfo

修订版86b23c6decb1fd0261cd67ffe6d3e9771fefb7fc (tree)
时间2017-03-24 06:14:49
作者Eric Hopper <hopper@omni...>
CommiterEric Hopper

Log Message

Use Moody Camel's lock free queue implementation.

更改概述

差异

diff -r 450c4661537c -r 86b23c6decb1 test_thread.cpp
--- a/test_thread.cpp Thu Mar 23 10:27:44 2017 -0700
+++ b/test_thread.cpp Thu Mar 23 14:14:49 2017 -0700
@@ -2,6 +2,8 @@
22 #include <thread>
33 #include <future>
44 #include <iostream>
5+#include "readerwriterqueue.h"
6+#include <stdexcept>
57
68 extern void do_something();
79
@@ -27,17 +29,38 @@
2729 return count / interval;
2830 }
2931
32+
33+static bool end_thread_flag = false;
34+
35+void end_thread()
36+{
37+ end_thread_flag = true;
38+}
39+
40+void worker_thread(::moodycamel::BlockingReaderWriterQueue<the_call_t> &to_me,
41+ ::moodycamel::BlockingReaderWriterQueue<bool> &from_me)
42+{
43+ while (!end_thread_flag) {
44+ the_call_t a_call;
45+ to_me.wait_dequeue(a_call);
46+ a_call();
47+ if (!from_me.enqueue(true)) {
48+ throw ::std::runtime_error("Unable to queue notification!");
49+ }
50+ }
51+}
52+
3053 int main()
3154 {
3255 using ::std::cout;
3356 using ::std::async;
3457 using ::std::thread;
3558 using ::std::launch;
36- cout << " Do nothing calls per second: "
59+ cout << " Do nothing calls per second: "
3760 << calls_per_second([]() { }, 5) << '\n';
38- cout << " Empty calls per second: "
61+ cout << " Empty calls per second: "
3962 << calls_per_second([]() { do_something(); }, 5) << '\n';
40- cout << " New thread calls per second: "
63+ cout << " New thread calls per second: "
4164 << calls_per_second(
4265 []() {
4366 thread t{ do_something };
@@ -46,7 +69,7 @@
4669 5
4770 )
4871 << '\n';
49- cout << "Async launch calls per second: "
72+ cout << " Async launch calls per second: "
5073 << calls_per_second(
5174 []() {
5275 auto fut = async(launch::async | launch::deferred, do_something);
@@ -55,5 +78,28 @@
5578 5
5679 )
5780 << '\n';
81+ {
82+ ::moodycamel::BlockingReaderWriterQueue<the_call_t> from_main;
83+ ::moodycamel::BlockingReaderWriterQueue<bool> to_main;
84+ thread worker{ [&from_main, &to_main]() { worker_thread(from_main, to_main); } };
85+ cout << "Worker thread calls per second: "
86+ << calls_per_second(
87+ [&from_main, &to_main]() {
88+ if (!from_main.enqueue(do_something)) {
89+ throw ::std::runtime_error("Unable to send request to worker.");
90+ }
91+ bool dummy = false;
92+ to_main.wait_dequeue(dummy);
93+ },
94+ 5
95+ )
96+ << '\n';
97+ from_main.enqueue(end_thread);
98+ {
99+ bool dummy;
100+ to_main.wait_dequeue(dummy);
101+ }
102+ worker.join();
103+ }
58104 return 0;
59105 }