muduo网络库源码学习————线程类
muduo库里面的线程类是使用基于对象的编程思想,源码目录为muduo/base,如下所示:
线程类头文件:
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
//线程类
#ifndef MUDUO_BASE_THREAD_H
#define MUDUO_BASE_THREAD_H
#include <muduo/base/Atomic.h>
#include <muduo/base/Types.h>
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <pthread.h>
//线程类头文件
namespace muduo
{
class Thread : boost::noncopyable
{
public:
typedef boost::function<void ()> ThreadFunc;//函数适配接收的函数
//线程构造函数,参数为回调函数和线程名称
explicit Thread(const ThreadFunc&, const string& name = string());//名称默认值为空的字符串类
//线程析构函数
~Thread();
void start();//启动线程
int join(); // return pthread_join()
bool started() const { return started_; }//线程是否已经启动
// pthread_t pthreadId() const { return pthreadId_; }
pid_t tid() const { return tid_; }//线程的真实pid
const string& name() const { return name_; }//线程的名称
static int numCreated() { return numCreated_.get(); }//已经启动的线程个数
private:
static void* startThread(void* thread);//现成的入口函数,调用runInThread函数
void runInThread();//调用回调函数func_
bool started_;//线程是否已经启动
pthread_t pthreadId_;//线程的pthread_t
pid_t tid_;//线程真实的 pid
ThreadFunc func_;//线程的回调函数
string name_;//线程的名称
static AtomicInt32 numCreated_;//已经创建的线程的个数,每当创建一个线程,该值就加一(原子整数类)
};
}
#endif
线程类的实现文件:
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
//线程类实现文件
#include <muduo/base/Thread.h>
#include <muduo/base/CurrentThread.h>
#include <muduo/base/Exception.h>
//#include <muduo/base/Logging.h>
//暂时不用日志文件,先注释掉
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_same.hpp>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <linux/unistd.h>
namespace muduo
{
namespace CurrentThread
{//__thread两个下划线是gcc 内置的线程局部存储设施
//每个线程各有一个,并不会去共享他
//缓存获取tid是为了提高获取tid的效率
__thread int t_cachedTid = 0;//线程真实pid的缓存,如果每次都用系统调用去获取pid,效率会低
__thread char t_tidString[32];//tid的字符串表示形式
__thread const char* t_threadName = "unknown";//线程的名称
const bool sameType = boost::is_same<int, pid_t>::value;//如果是相同类型,返回true
BOOST_STATIC_ASSERT(sameType);//编译时断言
}
namespace detail
{
pid_t gettid()//通过系统调用SYS_gettid获得tid
{
return static_cast<pid_t>(::syscall(SYS_gettid));//类型转化为pid_t
}
void afterFork()//子进程调用的
{
muduo::CurrentThread::t_cachedTid = 0;//当前线程pid赋值0
muduo::CurrentThread::t_threadName = "main";//名称赋值name
CurrentThread::tid();//进行缓存
// no need to call pthread_atfork(NULL, NULL, &afterFork);
}
class ThreadNameInitializer
{
public:
ThreadNameInitializer()//构造函数
{
muduo::CurrentThread::t_threadName = "main";//线程名称赋为main,即为主线程名称
CurrentThread::tid();//缓存当前线程的pid
//#include <pthread.h>
//int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));
//调用fork时,内部创建子进程前在父进程中会调用prepare,
//内部创建子进程成功后,父进程会调用parent ,子进程会调用child
pthread_atfork(NULL, NULL, &afterFork);//如果使用fork函数,那么子进程会调用邋afterFork
}
};
ThreadNameInitializer init;
}
}
using namespace muduo;
void CurrentThread::cacheTid()
{
if (t_cachedTid == 0)
{
t_cachedTid = detail::gettid();//调用gettid函数获得tid
int n = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);//将tid格式化保存在t_tidString中
assert(n == 6);//断言长度是6,5d后面还有一个空格,所以是6
(void) n;//这一句主要是预防n没有使用从而产生警告
}
}
bool CurrentThread::isMainThread()
{
return tid() == ::getpid();//查看tid是否等于当前进程id
}
AtomicInt32 Thread::numCreated_;
//构造函数,初始化
Thread::Thread(const ThreadFunc& func, const string& n) : started_(false), pthreadId_(0),tid_(0),func_(func),name_(n)
{
numCreated_.increment();//创建的线程的个数加一,为原子性操作
}
Thread::~Thread()
{
// no join
}
void Thread::start()
{
assert(!started_);
started_ = true;
//创建线程,startThread为线程的入口函数
errno = pthread_create(&pthreadId_, NULL, &startThread, this);
if (errno != 0)
{//日志
// LOG_SYSFATAL << "Failed in pthread_create";
}
}
int Thread::join()
{
assert(started_);
return pthread_join(pthreadId_, NULL);
}
//线程的入口函数
void* Thread::startThread(void* obj)
{//this指针传到obj
Thread* thread = static_cast<Thread*>(obj);//转化为线程基类的指针
thread->runInThread();//调用线程函数runInThread
return NULL;
}
//被线程的入口函数调用
void Thread::runInThread()
{
tid_ = CurrentThread::tid();//获取线程的tid
muduo::CurrentThread::t_threadName = name_.c_str();//缓存该线程的名称
try
{
func_();//调用回调函数
muduo::CurrentThread::t_threadName = "finished";
}
catch (const Exception& ex)//异常捕捉
{
muduo::CurrentThread::t_threadName = "crashed";
fprintf(stderr, "exception caught in Thread %s\n", name_.c_str());
fprintf(stderr, "reason: %s\n", ex.what());
fprintf(stderr, "stack trace: %s\n", ex.stackTrace());
abort();
}
catch (const std::exception& ex)
{
muduo::CurrentThread::t_threadName = "crashed";
fprintf(stderr, "exception caught in Thread %s\n", name_.c_str());
fprintf(stderr, "reason: %s\n", ex.what());
abort();
}
catch (...)
{
muduo::CurrentThread::t_threadName = "crashed";
fprintf(stderr, "unknown exception caught in Thread %s\n", name_.c_str());
throw; // rethrow
}
}
CurrentThread头文件
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (chenshuo at chenshuo dot com)
#ifndef MUDUO_BASE_CURRENTTHREAD_H
#define MUDUO_BASE_CURRENTTHREAD_H
namespace muduo
{//CurrentThread的名称空间
namespace CurrentThread
{
// internal
extern __thread int t_cachedTid;
extern __thread char t_tidString[32];
extern __thread const char* t_threadName;
void cacheTid();
inline int tid()
{
if (t_cachedTid == 0)//还没有缓存过
{//t_cachedTid初值 是0
cacheTid();//进行缓存
}
return t_cachedTid;//返回缓存的tid
}
inline const char* tidString() // for logging
{
return t_tidString;//返回tid的字符串表示形式
}
inline const char* name()
{
return t_threadName;//返回线程名称
}
bool isMainThread();//是否是主线程
}
}
#endif
测试代码位于muduo/base/tests
//线程测试程序
#include <muduo/base/Thread.h>
#include <muduo/base/CurrentThread.h>
#include <string>
#include <boost/bind.hpp>
#include <stdio.h>
void threadFunc()
{
printf("tid=%d\n", muduo::CurrentThread::tid());
}
void threadFunc2(int x)
{
printf("tid=%d, x=%d\n", muduo::CurrentThread::tid(), x);
}
class Foo
{
public:
explicit Foo(double x) : x_(x)
{
}
void memberFunc()
{
printf("tid=%d, Foo::x_=%f\n", muduo::CurrentThread::tid(), x_);
}
void memberFunc2(const std::string& text)
{
printf("tid=%d, Foo::x_=%f, text=%s\n", muduo::CurrentThread::tid(), x_, text.c_str());
}
private:
double x_;
};
int main()
{//获取当前线程的pid(进程id 线程pid)
printf("pid=%d, tid=%d\n", ::getpid(), muduo::CurrentThread::tid());
//创建一个线程对象,传递一个函数
muduo::Thread t1(threadFunc);
t1.start();//启动线程
t1.join();
//threadFunc2带了一个参数,用boost::bind函数传递进去,最后是线程的名称,可以不传
muduo::Thread t2(boost::bind(threadFunc2, 42), "thread for free function with argument");
t2.start();
t2.join();
//创建一个对象
Foo foo(87.53);
//创建第三个线程(成员函数的话一定要用&)
muduo::Thread t3(boost::bind(&Foo::memberFunc, &foo), "thread for member function without argument");
t3.start();
t3.join();
//创建第四个线程,这里传进去的函数是带参数的
muduo::Thread t4(boost::bind(&Foo::memberFunc2, boost::ref(foo), std::string("Shuo Chen")));
t4.start();
t4.join();
//打印最后创建的线程总数
printf("number of created threads %d\n", muduo::Thread::numCreated());
}
单独编译后运行结果如下:
muduo网络库源码学习————线程类的更多相关文章
- muduo网络库源码学习————线程本地单例类封装
muduo库中线程本地单例类封装代码是ThreadLocalSingleton.h 如下所示: //线程本地单例类封装 // Use of this source code is governed b ...
- muduo网络库源码学习————线程池实现
muduo库里面的线程池是固定线程池,即创建的线程池里面的线程个数是一定的,不是动态的.线程池里面一般要包含线程队列还有任务队列,外部程序将任务存放到线程池的任务队列中,线程池中的线程队列执行任务,也 ...
- muduo网络库源码学习————线程特定数据
muduo库线程特定数据源码文件为ThreadLocal.h //线程本地存储 // Use of this source code is governed by a BSD-style licens ...
- muduo网络库源码学习————日志类封装
muduo库里面的日志使方法如下 这里定义了一个宏 #define LOG_INFO if (muduo::Logger::logLevel() <= muduo::Logger::INFO) ...
- muduo网络库源码学习————线程安全
线程安全使用单例模式,保证了每次只创建单个对象,代码如下: Singleton.h // Use of this source code is governed by a BSD-style lice ...
- muduo网络库源码学习————Exception类
Exception类是为异常捕获而设计,可以获得异常的信息以及栈的回溯信息 (原来的代码没有demangle成员函数,输出的格式比较难看,加了demangle成员函数,利用demangle成员函数可以 ...
- muduo网络库源码学习————Timestamp.cc
今天开始学习陈硕先生的muduo网络库,moduo网络库得到很多好评,陈硕先生自己也说核心代码不超过5000行,所以我觉得有必要拿过来好好学习下,学习的时候在源码上面添加一些自己的注释,方便日后理解, ...
- muduo网络库源码学习————互斥锁
muduo源码的互斥锁源码位于muduo/base,Mutex.h,进行了两个类的封装,在实际的使用中更常使用MutexLockGuard类,因为该类可以在析构函数中自动解锁,避免了某些情况忘记解锁. ...
- muduo网络库源码学习————日志滚动
muduo库里面的实现日志滚动有两种条件,一种是日志文件大小达到预设值,另一种是时间到达超过当天.滚动日志类的文件是LogFile.cc ,LogFile.h 代码如下: LogFile.cc #in ...
随机推荐
- 项目中你不得不知的11个Java第三方类库
项目中你不得不知的11个Java第三方类库 博客分类: Java综合 JavaGoogle框架单元测试Hibernate Java第三方library ecosystem是一个很广阔的范畴.不久前有人 ...
- Java包机制和Javadoc的使用
1.什么是包机制? 包(package)其实本质上就是一个文件夹,使用包是为了让相同类名的两个类可以使用,也就是操作系统中的文件夹,用来解决重名并且让相同的功能类放在同一个包,使开发更加有条理. 注意 ...
- unity3d之简单动画
Unity3d中有两个关于动画的概念,Animation和Animator,看一下他们的创建和区别 1.创建一个物体后可以添加Animator和Animation组件如图所示 2.Animation和 ...
- Hadoop学习笔记(2)-HDFS的基本操作(Shell命令)
在这里我给大家继续分享一些关于HDFS分布式文件的经验哈,其中包括一些hdfs的基本的shell命令的操作,再加上hdfs java程序设计.在前面我已经写了关于如何去搭建hadoop这样一个大数据平 ...
- spring源码阅读笔记08:bean加载之创建bean
上文从整体视角分析了bean创建的流程,分析了Spring在bean创建之前所做的一些准备工作,并且简单分析了一下bean创建的过程,接下来就要详细分析bean创建的各个流程了,这是一个比较复杂的过程 ...
- Personal Photo Management Application
Customer Problems & Needs People may take a large number of photos and their phone don't have en ...
- asp.net mvc 接收jquery ajax发送的数组对象
<script type="text/javascript"> $(function () { var obj = { name: "军需品", m ...
- Java - window下环境配置
JDK下载 官网:https://www.oracle.com/java/technologies/javase-jdk8-downloads.html 百度网盘: 链接:https://pan.ba ...
- ORA-0245
经常有客户报错ORA-0245 1.11.2 rac环境, rman存在snap控制文件路径,默认是文件系统[非共享,导致备份控制文件报错] 解决方法:将snap路径配置到ASM磁盘组共享路径[nfs ...
- Celery实现周期任务
这个翻译之后居然叫芹菜~~最近Django框架需要涉及到执行周期任务~~上网搜了下其实还挺多的(django_crontab:这个学习周期短,但是发现不仅麻烦还不好用啊).(apscheduler,简 ...