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.

55 lines
1.5KB

  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <csignal>
  11. #include <atomic>
  12. #include <iostream>
  13. #include "Util/logger.h"
  14. #include "Util/TimeTicker.h"
  15. #include "Thread/ThreadPool.h"
  16. using namespace std;
  17. using namespace toolkit;
  18. int main() {
  19. signal(SIGINT,[](int ){
  20. exit(0);
  21. });
  22. //初始化日志系统
  23. Logger::Instance().add(std::make_shared<ConsoleChannel> ());
  24. atomic_llong count(0);
  25. ThreadPool pool(1,ThreadPool::PRIORITY_HIGHEST, false);
  26. Ticker ticker;
  27. for (int i = 0 ; i < 1000*10000;++i){
  28. pool.async([&](){
  29. if(++count >= 1000*10000){
  30. InfoL << "执行1000万任务总共耗时:" << ticker.elapsedTime() << "ms";
  31. }
  32. });
  33. }
  34. InfoL << "1000万任务入队耗时:" << ticker.elapsedTime() << "ms" << endl;
  35. uint64_t lastCount = 0 ,nowCount = 1;
  36. ticker.resetTime();
  37. //此处才开始启动线程
  38. pool.start();
  39. while (true){
  40. sleep(1);
  41. nowCount = count.load();
  42. InfoL << "每秒执行任务数:" << nowCount - lastCount;
  43. if(nowCount - lastCount == 0){
  44. break;
  45. }
  46. lastCount = nowCount;
  47. }
  48. return 0;
  49. }