(13)muduo_base库源码分析:Thread类实现

2024-06-08 06:32

本文主要是介绍(13)muduo_base库源码分析:Thread类实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 1.线程标识符
    • 2.Thread类图
    • 3.Thread类相关代码学习
    • 4.相关测试
    • 5.多线程多进程的死锁案例

1.线程标识符

  • Linux中,每个进程有一个pid,类型pid_t,由getpid()取得。Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库维护,其id空间是各个进程独立的(即不同进程中的线程可能有相同的id)。
    Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
  • 有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid。
  • 有一个函数gettid()可以得到tid,但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取。
return syscall(SYS_gettid)

2.Thread类图

  • typedef boost::function<void ()> ThreadFunc;
    在这里插入图片描述
  • __thread,gcc内置的线程局部存储设施
(1)__thread只能修饰POD类型
POD类型(plain old data),与C兼容的原始数据,例如,结构和整型等C语言中的类型是 POD 类型(初始化只能是编译期常量),
但带有用户定义的构造函数或虚函数的类则不是
__thread string t_obj1(“cppcourse”);	// 错误,不能调用对象的构造函数
__thread string* t_obj2 = new string;	// 错误,初始化只能是编译期常量,指针类型是POD类型,但是new也是需要调用构造函数的,
不能是运行期的
__thread string* t_obj3 = NULL;	// 正确(2)非POD类型,也希望每个线程只有1份,该怎么办?
可以用线程特定数据tsd

3.Thread类相关代码学习

  • 13\jmuduo\muduo\base\Thread.h
// 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_EXCEPTION_H
#define MUDUO_BASE_EXCEPTION_H#include <muduo/base/Types.h>
#include <exception>namespace muduo
{class Exception : public std::exception
{public:explicit Exception(const char* what);explicit Exception(const string& what);virtual ~Exception() throw();virtual const char* what() const throw();const char* stackTrace() const throw();private:void fillStackTrace();string demangle(const char* symbol);string message_;string stack_;
};}#endif  // MUDUO_BASE_EXCEPTION_H
  • 13\jmuduo\muduo\base\Thread.cc
// 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修饰的变量是线程局部存储的。多个线程并不会去共享他,每个线程都有1份__thread int t_cachedTid = 0;		// 线程真实pid(tid)的缓存,// 是为了减少::syscall(SYS_gettid)系统调用的次数// 提高获取tid的效率__thread char t_tidString[32];	// 这是tid的字符串表示形式__thread const char* t_threadName = "unknown";//线程的名称const bool sameType = boost::is_same<int, pid_t>::value;//boost::is_same作用:若int和pid_t是相同类型的,则返回是trueBOOST_STATIC_ASSERT(sameType);
}namespace detail
{pid_t gettid()
{return static_cast<pid_t>(::syscall(SYS_gettid));
}void afterFork()
{muduo::CurrentThread::t_cachedTid = 0;//子进程中当前线程的tid=0muduo::CurrentThread::t_threadName = "main";CurrentThread::tid();//缓存一下tid// no need to call pthread_atfork(NULL, NULL, &afterFork);
}/*
(1)namespace detail中的pthread_atfork意义在于:
fork可能是在主线程中调用,也可能是在子线程中调用,
fork得到一个新进程,新进程中只有1个执行序列,只有1个线程(调用fork的线程被继承下来)(2)实际上,对于编写多线程程序来说,我们最好不要调用fork,不要编写多线程多进程程序,
要么是用多进程,要么用多线程
*/class ThreadNameInitializer
{public:ThreadNameInitializer(){muduo::CurrentThread::t_threadName = "main";//主线程的名称为mainCurrentThread::tid();//缓存当前线程的tid到t_cachedTid中pthread_atfork(NULL, NULL, &afterFork);//若调用成功,则子进程则会调用afterFork}
};ThreadNameInitializer init;//在detail名称空间中,先于main函数
}
}using namespace muduo;void CurrentThread::cacheTid()
{if (t_cachedTid == 0){t_cachedTid = detail::gettid();//detail名称空间的gettid()int n = snprintf(t_tidString, sizeof t_tidString, "%5d ", t_cachedTid);assert(n == 6); (void) n;//(void) n作用:防止release版本编译不通过,因为release版本编译的时候,assert(n == 6)这条语句//相当于没有,那么上一条语句的n相当于没用}
}bool CurrentThread::isMainThread()
{return tid() == ::getpid();
}AtomicInt32 Thread::numCreated_;Thread::Thread(const ThreadFunc& func, const string& n): started_(false),pthreadId_(0),tid_(0),func_(func),name_(n)
{numCreated_.increment();//原子操作,线程个数+1
}Thread::~Thread()
{// no join
}void Thread::start()
{assert(!started_);started_ = true;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);
}//线程的入口函数,this指针传递进来
void* Thread::startThread(void* obj)
{Thread* thread = static_cast<Thread*>(obj);thread->runInThread();return NULL;
}void Thread::runInThread()
{tid_ = CurrentThread::tid();//获取线程tidmuduo::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}
}
  • 13\jmuduo\muduo\base\CurrentThread.h
// 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_Hnamespace muduo
{
namespace CurrentThread
{// internalextern __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)//若没缓存tid,则获取tid{cacheTid();}return t_cachedTid;//缓存好后,直接将tid返回}inline const char* tidString() // for logging{return t_tidString;}inline const char* name(){return t_threadName;}bool isMainThread();
}
}#endif

4.相关测试

  • 13\jmuduo\muduo\base\tests\Thread_test.cc
#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()
{printf("pid=%d, tid=%d\n", ::getpid(), muduo::CurrentThread::tid());//获取进程pid,获取线程tidmuduo::Thread t1(threadFunc);t1.start();t1.join();muduo::Thread t2(boost::bind(threadFunc2, 42),"thread for free function with argument");//"thread for free function with argument"可以不传到构造函数,因为有默认值t2.start();t2.join();Foo foo(87.53);//调用不带参数的成员函数memberFuncmuduo::Thread t3(boost::bind(&Foo::memberFunc, &foo),"thread for member function without argument");t3.start();t3.join();//调用带参数的成员函数memberFunc2//等价于:muduo::Thread t4(boost::bind(&Foo::memberFunc2, &foo, std::string("Shuo Chen")));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());
}
  • 相关目录如下
    在这里插入图片描述
13\jmuduo\muduo\base\CMakeLists.txt
set(base_SRCSException.ccThread.cc  ##新增Timestamp.cc)
..........
===============================================
13\jmuduo\muduo\base\tests\CMakeLists.txt
.........
##新增
add_executable(thread_test Thread_test.cc)
target_link_libraries(thread_test muduo_base)
##新增
........
  • 执行结果如下所示:
    在这里插入图片描述

5.多线程多进程的死锁案例

  • 目录如下:
    在这里插入图片描述
13\jmuduo\tests\CMakeLists.txt##新增
add_executable(deadlock_test Deadlock_test.cc)
target_link_libraries(deadlock_test pthread)add_executable(deadlock_test2 Deadlock_test2.cc)
target_link_libraries(deadlock_test2 pthread)add_executable(pthread_atfork_test Pthread_atfork_test.cc)
target_link_libraries(pthread_atfork_test pthread)
##新增
  • 死锁eg代码:13\jmuduo\tests\Deadlock_test.cc
// 一个在多线程程序里fork造成死锁的例子
// 一个输出示例:
/*pid = 19445 Entering main ...
pid = 19445 begin doit ...
pid = 19447 begin doit ...
pid = 19445 end doit ...
pid = 19445 Exiting main ...父进程在创建了一个线程,并对mutex加锁,
父进程创建一个子进程,在子进程中调用doit,由于子进程会复制父进程的内存,这时候mutex处于锁的状态,
父进程在复制子进程的时候,只会复制当前线程的执行状态,其它线程不会复制。因此子进程会处于死锁的状态。
*/
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;void* doit(void* arg)
{printf("pid = %d begin doit ...\n",static_cast<int>(getpid()));pthread_mutex_lock(&mutex);//已经是锁了,又调用了lock函数会造成死锁,2次加锁struct timespec ts = {2, 0};//等待2snanosleep(&ts, NULL);pthread_mutex_unlock(&mutex);printf("pid = %d end doit ...\n",static_cast<int>(getpid()));return NULL;
}int main(void)
{printf("pid = %d Entering main ...\n", static_cast<int>(getpid()));pthread_t tid;pthread_create(&tid, NULL, doit, NULL);//会先调用doit函数,然后睡眠2sstruct timespec ts = {1, 0};//等待1snanosleep(&ts, NULL);if (fork() == 0)//fork后,子进程会拷贝父进程所有的内存,mutex也会被拷贝一份,子进程拷贝下来就已经处于加锁状态{doit(NULL);//子进程运行到这里}pthread_join(tid, NULL);//父进程运行到这里printf("pid = %d Exiting main ...\n",static_cast<int>(getpid()));return 0;
}
  • 执行deadlock_test后,该进程不会退出,处于死锁状态
    在这里插入图片描述

  • 不死锁的代码eg:13\jmuduo\tests\Deadlock_test2.cc
    利用pthread_atfork解决

#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;void* doit(void* arg)
{printf("pid = %d begin doit ...\n",static_cast<int>(getpid()));pthread_mutex_lock(&mutex);struct timespec ts = {2, 0};nanosleep(&ts, NULL);pthread_mutex_unlock(&mutex);printf("pid = %d end doit ...\n",static_cast<int>(getpid()));return NULL;
}void prepare(void)
{pthread_mutex_unlock(&mutex);
}void parent(void)
{pthread_mutex_lock(&mutex);
}int main(void)
{pthread_atfork(prepare, parent, NULL);printf("pid = %d Entering main ...\n", static_cast<int>(getpid()));pthread_t tid;pthread_create(&tid, NULL, doit, NULL);struct timespec ts = {1, 0};nanosleep(&ts, NULL);if (fork() == 0)//子进程首先会调用prepare,使得mutex处于解锁状态,所以子进程拷贝的是解锁状态mutex{doit(NULL);//调用doit,再加锁,解锁}//由于子进程先调用prepare函数,导致mutex为解锁状态,那么父进程再调用parent函数,进行mutex加锁,则不会造成死锁pthread_join(tid, NULL);printf("pid = %d Exiting main ...\n",static_cast<int>(getpid()));return 0;
}
  • 结果如下:
    在这里插入图片描述

这篇关于(13)muduo_base库源码分析:Thread类实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1041423

相关文章

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南

《SpringBoot集成EasyExcel实现百万级别的数据导入导出实践指南》本文将基于开源项目springboot-easyexcel-batch进行解析与扩展,手把手教大家如何在SpringBo... 目录项目结构概览核心依赖百万级导出实战场景核心代码效果百万级导入实战场景监听器和Service(核心

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的