zoukankan      html  css  js  c++  java
  • muduo: Exception Thread Mutex

    注释过的源码太多就不放在这,可以去GitHub查看

    下面是测试程序:

    #include "Exception.h"
    #include <iostream>
    
    class Bar {
    public:
      void test() {
        throw::muduo::Exception("oops"); // 抛出异常
      }  
    };
    
    void foo() {
      Bar b;
      b.test();
    }
    
    int main()
    {
      try {
        foo(); // 运行此函数 会抛出 oops 异常
      }
      catch (const muduo::Exception& ex) {  // 捕捉异常 并打印异常信息
        std::cout << "reason: " << ex.what() << std::endl;
        std::cout << "stack trace: " << ex.stackTrace() << std::endl;
      }
    
      return 0;
    }
    #include <iostream>
    #include <string>
    #include <functional>
    #include <stdio.h>
    #include <unistd.h>
    #include "Thread.h"
    #include "CurrentThread.h"
    
    void threadFunc() {
      std::cout << "tid = " << muduo::CurrentThread::tid() << std::endl;
    }
    
    void threadFunc2(int x) {
      std::cout << "tid = " << muduo::CurrentThread::tid() << " x = " << x << std::endl;
    }
    
    class Foo {
    public:
      explicit Foo(double x) : x_(x) {}
      
      void memberFunc2(const std::string& text) {
        std::cout << "tid = " << muduo::CurrentThread::tid() << " Foo::x_ = " << x_ << " text = " << text << std::endl;
      }
    
    private:
      double x_;
    };
    
    int main()
    {
      std::cout << "pid = " << ::getpid() << " tid = " << muduo::CurrentThread::tid() << std::endl;
    
      muduo::Thread t1(threadFunc);
      t1.start();
      t1.join();
    
      muduo::Thread t2(std::bind(threadFunc2, 42), "thread for free function with argument");
      t2.start();
      t2.join();
    
      Foo foo(87.35);
      //muduo::Thread t3(std::bind(&Foo::memberFunc2, &foo);
      //t3.start();
      //t3.join();
    
      muduo::Thread t4(std::bind(&Foo::memberFunc2, &foo, "thread for member function with argument"));
      t4.start();
      t4.join();
    
      std::cout << "number of created threads " << muduo::Thread::numCreated() << std::endl;
    
      return 0;
    }
    #include "Mutex.h"
    #include "Thread.h"
    #include "Timestamp.h"
    
    #include <functional>
    #include <vector>
    #include <iostream>
    #include <boost/ptr_container/ptr_vector.hpp>
    using namespace muduo;
    using namespace std;
    
    MutexLock g_mutex;
    vector<int> g_vec;
    const int kCount = 10*1000*1000;
    
    void threadFunc() {
      for (int i = 0; i < kCount; i++) {
        MutexLockGuard lock(g_mutex);
        g_vec.push_back(i);
      }
    }
    
    int main()
    {
      const int kMaxThreads = 8;
      g_vec.reserve(kMaxThreads * kCount);
    
      Timestamp start(Timestamp::now());
    
      // 仅仅push_back
      for (int i = 0; i < kCount; i++) {
        g_vec.push_back(i);
      }
    
      // push_back kCount个数所用的时间
      cout << "single thread without lock " << timeDifference(Timestamp::now(), start) << endl;
    
      start = Timestamp::now();
    
      // push_back + MutexLockGuard的构造析构
      threadFunc(); // 
      cout << "single thread with lock " << timeDifference(Timestamp::now(), start) << endl;
    
      for (int nthreads = 1; nthreads < kMaxThreads; ++nthreads) {
        boost::ptr_vector<Thread> threads;
        g_vec.clear();
        start = Timestamp::now();
    
        for (int i = 0; i < nthreads; ++i) {
          threads.push_back(new Thread(&threadFunc));
          threads.back().start();
        }
    
        for (int i = 0; i < nthreads; ++i) {
          threads[i].join();
        }
    
        cout << nthreads << " threads with lock " << timeDifference(Timestamp::now(), start) << endl;
      }
    
      return 0;
    }
    
    // g++ Timestamp.cc CountDownLatch.cc CurrentThread.cc Thread.cc Mutex_test.cc -o Mutex_test -lpthread
    转载请注明出处
  • 相关阅读:
    Java中子类继承了父类的私有属性及方法吗?
    为什么静态成员、静态方法中不能用this和super关键字
    poj 3378 二维树状数组
    poj 3034 动态规划
    poj 2498 动态规划
    poj 2029 二维树状数组
    hdu 3280 动态规划
    hdu 2586 LCA
    poj 3689 树形dp
    poj 1947 树形dp
  • 原文地址:https://www.cnblogs.com/lnlin/p/14554012.html
Copyright © 2011-2022 走看看