diff --git a/lib_fiber/samples-c++1x/fibers/CMakeLists.txt b/lib_fiber/samples-c++1x/fibers/CMakeLists.txt index c74b0458a..cb910fc16 100644 --- a/lib_fiber/samples-c++1x/fibers/CMakeLists.txt +++ b/lib_fiber/samples-c++1x/fibers/CMakeLists.txt @@ -81,6 +81,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Darwin") set(lib_all ${libfiber_cpp} ${libacl_all} ${libfiber} -liconv -lz -lpthread -ldl) elseif(CMAKE_SYSTEM_NAME MATCHES "Linux") set(lib_all ${libfiber_cpp} ${libacl_all} ${libfiber} -lz -lpthread -ldl) +# set(lib_all ${libfiber_cpp} ${libacl_all} ${libfiber} -lz -lpthread -ldl -ljemalloc) endif() set(output_path ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/lib_fiber/samples-c++1x/fibers/fiber_pool2.h b/lib_fiber/samples-c++1x/fibers/fiber_pool2.h index c583925ef..18f8a950a 100644 --- a/lib_fiber/samples-c++1x/fibers/fiber_pool2.h +++ b/lib_fiber/samples-c++1x/fibers/fiber_pool2.h @@ -44,6 +44,12 @@ class fiber_pool2 { return res; } + template + void exec2(Fn&& fn, Args&&... args) { + auto obj = std::bind(std::forward(fn), std::forward(args)...); + boxes_[next_++ % boxes_.size()]->push(obj, true); + } + void stop() { for (auto fb : fibers_) { acl_fiber_kill(fb); diff --git a/lib_fiber/samples-c++1x/fibers/main.cpp b/lib_fiber/samples-c++1x/fibers/main.cpp index 9796970d1..69b6a48ed 100644 --- a/lib_fiber/samples-c++1x/fibers/main.cpp +++ b/lib_fiber/samples-c++1x/fibers/main.cpp @@ -73,16 +73,21 @@ static void task_run(std::atomic_long &total, long long i) { } static void test2(long long count, int buf, int concurrency, - int milliseconds, bool thr) { + int milliseconds, bool thr, bool use_exec2) { std::shared_ptr fibers = std::make_shared (buf, concurrency, milliseconds, thr); std::atomic_long total(0); - long long delay = thr ? 10000 : 100000000; + long long delay = thr ? 10000 : 1000000; - go[fibers, count, delay, &total] { + go[fibers, count, delay, &total, use_exec2] { for (long long i = 0; i < count; i++) { - fibers->exec(task_run, std::ref(total), i); + if (use_exec2) { + fibers->exec2(task_run, std::ref(total), i); + } else { + fibers->exec(task_run, std::ref(total), i); + } + if (i % delay == 0) { acl::fiber::yield(); } @@ -103,7 +108,8 @@ static void test2(long long count, int buf, int concurrency, long long cnt = total.load(); double tc = acl::stamp_sub(end, begin); double speed = (cnt * 1000) / tc; - printf("total result: %lld, tc: %.2f ms, speed: %.2f qps\r\n", cnt, tc, speed); + printf("%s total result: %lld, tc: %.2f ms, speed: %.2f qps\r\n", + use_exec2 ? "exec2" : "exec", cnt, tc, speed); } ////////////////////////////////////////////////////////////////////////////// @@ -115,19 +121,20 @@ static void usage(const char* procname) { " -c concurrency\r\n" " -q qlen\r\n" " -t timeout\r\n" + " -D [if use exec for testing fiber_pool2]\r\n" " -T [if using in threads]\r\n", procname); } int main(int argc, char *argv[]) { int ch; - bool thr = false; + bool thr = false, use_exec2 = true; int buf = 500, concurrency = 10, qlen = 0, milliseconds = -1; long long count = 100; acl::acl_cpp_init(); acl::log::stdout_open(true); - while ((ch = getopt(argc, argv, "hn:b:c:q:t:T")) > 0) { + while ((ch = getopt(argc, argv, "hn:b:c:q:t:TD")) > 0) { switch (ch) { case 'h': usage(argv[0]); @@ -150,6 +157,9 @@ int main(int argc, char *argv[]) { case 'T': thr = true; break; + case 'D': + use_exec2 = false; + break; default: break; } @@ -159,6 +169,6 @@ int main(int argc, char *argv[]) { printf("-----------------------------------------------------------\r\n"); - test2(count, buf, concurrency, milliseconds, thr); + test2(count, buf, concurrency, milliseconds, thr, use_exec2); return 0; }