注释过的源码太多就不放在这,可以去​​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

 

转载请注明出处