|
| 1 | +#include "threadpool.h" |
| 2 | +#include <iostream> |
| 3 | +#include <windows.h> |
| 4 | + |
| 5 | + |
| 6 | +void fun1(int slp) |
| 7 | +{ |
| 8 | + printf(" hello, fun1 ! %d\n" ,std::this_thread::get_id()); |
| 9 | + if (slp>0) { |
| 10 | + printf(" ======= fun1 sleep %d ========= %d\n",slp, std::this_thread::get_id()); |
| 11 | + std::this_thread::sleep_for(std::chrono::milliseconds(slp)); |
| 12 | + //Sleep(slp ); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +struct gfun { |
| 17 | + int operator()(int n) { |
| 18 | + printf("%d hello, gfun ! %d\n" ,n, std::this_thread::get_id() ); |
| 19 | + return 42; |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +class A { //函数必须是 static 的才能使用线程池 |
| 24 | +public: |
| 25 | + static int Afun(int n = 0) { |
| 26 | + std::cout << n << " hello, Afun ! " << std::this_thread::get_id() << std::endl; |
| 27 | + return n; |
| 28 | + } |
| 29 | + |
| 30 | + static std::string Bfun(int n, std::string str, char c) { |
| 31 | + std::cout << n << " hello, Bfun ! "<< str.c_str() <<" " << (int)c <<" " << std::this_thread::get_id() << std::endl; |
| 32 | + return str; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +int main() |
| 37 | + try { |
| 38 | + std::threadpool executor{ 50 }; |
| 39 | + A a; |
| 40 | + std::future<void> ff = executor.commit(fun1,0); |
| 41 | + std::future<int> fg = executor.commit(gfun{},0); |
| 42 | + std::future<int> gg = executor.commit(a.Afun, 9999); //IDE提示错误,但可以编译运行 |
| 43 | + std::future<std::string> gh = executor.commit(A::Bfun, 9998,"mult args", 123); |
| 44 | + std::future<std::string> fh = executor.commit([]()->std::string { std::cout << "hello, fh ! " << std::this_thread::get_id() << std::endl; return "hello,fh ret !"; }); |
| 45 | + |
| 46 | + std::cout << " ======= sleep ========= " << std::this_thread::get_id() << std::endl; |
| 47 | + std::this_thread::sleep_for(std::chrono::microseconds(900)); |
| 48 | + |
| 49 | + for (int i = 0; i < 50; i++) { |
| 50 | + executor.commit(fun1,i*100 ); |
| 51 | + } |
| 52 | + std::cout << " ======= commit all ========= " << std::this_thread::get_id()<< " idlsize="<<executor.idlCount() << std::endl; |
| 53 | + |
| 54 | + std::cout << " ======= sleep ========= " << std::this_thread::get_id() << std::endl; |
| 55 | + std::this_thread::sleep_for(std::chrono::seconds(3)); |
| 56 | + |
| 57 | + ff.get(); //调用.get()获取返回值会等待线程执行完,获取返回值 |
| 58 | + std::cout << fg.get() << " " << fh.get().c_str()<< " " << std::this_thread::get_id() << std::endl; |
| 59 | + |
| 60 | + std::cout << " ======= sleep ========= " << std::this_thread::get_id() << std::endl; |
| 61 | + std::this_thread::sleep_for(std::chrono::seconds(3)); |
| 62 | + |
| 63 | + std::cout << " ======= fun1,55 ========= " << std::this_thread::get_id() << std::endl; |
| 64 | + executor.commit(fun1,55).get(); //调用.get()获取返回值会等待线程执行完 |
| 65 | + |
| 66 | + std::cout << "end... " << std::this_thread::get_id() << std::endl; |
| 67 | + |
| 68 | + |
| 69 | + std::threadpool pool(4); |
| 70 | + std::vector< std::future<int> > results; |
| 71 | + |
| 72 | + for (int i = 0; i < 8; ++i) { |
| 73 | + results.emplace_back( |
| 74 | + pool.commit([i] { |
| 75 | + std::cout << "hello " << i << std::endl; |
| 76 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 77 | + std::cout << "world " << i << std::endl; |
| 78 | + return i*i; |
| 79 | + }) |
| 80 | + ); |
| 81 | + } |
| 82 | + std::cout << " ======= commit all2 ========= " << std::this_thread::get_id() << std::endl; |
| 83 | + |
| 84 | + for (auto && result : results) |
| 85 | + std::cout << result.get() << ' '; |
| 86 | + std::cout << std::endl; |
| 87 | + return 0; |
| 88 | + } |
| 89 | +catch (std::exception& e) { |
| 90 | + std::cout << "some unhappy happened... " << std::this_thread::get_id() << e.what() << std::endl; |
| 91 | +} |
0 commit comments