信息发布→ 登录 注册 退出

【Linux课程学习】:锁封装(Mutex)线程封装(Thread),this指针

发布时间:2025-04-17

点击量:

linux学习笔记: https://blog.csdn.net/djdjiejsn/category_12669243.html

前言:

下面关于锁的封装看看下面的:

Lockguard的构造函数加锁,析构函数解锁,用起来很方便。

代码语言:javascript代码运行次数:0运行复制
namespace MutexModule{    class Mutex    {    private:        Mutex(const Mutex &) = delete;        const Mutex &operator=(const Mutex &) = delete;    public:        Mutex()        {            int n = ::pthread_mutex_init(&_mutex, nullptr);            (void)n;        }        void Lock()        {            int n = ::pthread_mutex_lock(&_mutex);            (void)n;        }        void UnLock()        {            int n = ::pthread_mutex_unlock(&_mutex);            (void)n;        }        pthread_mutex_t *LockPtr()        {            return &_mutex;        }        ~Mutex()        {            int n = ::pthread_mutex_destroy(&_mutex);            (void)n;        }    private:        pthread_mutex_t _mutex;    };    class LockGuard    {    public:        LockGuard(Mutex &mtx) : _mtx(mtx)        {            _mtx.Lock();        }        ~LockGuard()        {            _mtx.UnLock();        }    private:        Mutex &_mtx;    };}
1.访问内部私有成员变量

1.类中的static函数为什么不能直接访问内部成员变量?

因为默认没有传this指针。所以static函数参数中有this指针的时候,还是可以访问的,此时this(对象)指针不能省略。

2.外部函数不能访问私有成员?

没有默认传递this指针,没有访问权限。声明是类的友元函数的时候,也是可以访问的。

代码:

代码语言:javascript代码运行次数:0运行复制
#include using namespace std;class Solution {public:Solution():_n(1111){}friend static void test(Solution* t);static void func(){}static void a(){//可以访问静态成员方法,访问权限给到了private// 就是没有默认传递this指针而已func();_m = 11;}static void b(Solution* t){//可以访问静态成员方法,访问权限给到了private// 就是没有默认传递this指针而已t->_n = 555;t->_m;t->func();}private:int _n;static int _m;};void test(Solution* t){//能访问是传递了Solution对象,是Solution类的友元//friend static void test(Solution*& t);t->_n = 222;}

2.Thread成员变量:2.1.首先肯定是线程的tid:pthread_t _tid。2.2.进程的pid:pid_t _pid;。2.3.线程的状态:

表示线程的状态,NEW表示新的线程,还没有进行Start操作时的状态信息。

2.4.是否被分离:

线程默认是没有被分离的,而且只有分离和没有被分离两种情况,bool类型。

代码语言:javascript代码运行次数:0运行复制
void EnableDetach(){    //状态变为false,新的线程默认为true    _joinable=false;}void Detach(){    EnableDetach();    //线程分离    pthread_detach(_tid);}
2.5.线程名字

为了区分不同的线程可以给线程取个名字。

名字可以取"thread-num"表示是第几个线程。

2.6.执行的方法:

这里定义的是void(std::string name),返回值为void,参数是string的函数。


3.内部成员方法:3.1构造函数:

_name:

线程名字以"thread-num"来命名,表示第几个线程。所以定义一个static int num进行记录有多少个线程。但是这是临界资源,线程再创建线程,导致方式混乱。

_joinable:

_joinable默认是没有被分离的,所以是true。

_func:

_func是外部传进来要执行的方法。所以要有func_t 参数。

_status:

为新线程,状态为NEW。

_tid:

在线程创建的时候,传到pthread里面进行确定。

代码语言:javascript代码运行次数:0运行复制
Thread(func_t func)       :_pid(getpid())       ,_func(func)       ,_joinable(true)       ,_status(STATUS::NEW){      LockGuard lockguard(_lock);      {       _name="thread-"+std::to_string(num++);        }}
3.2Start函数:

先判断是不是没有在RUNNING,如果在RUNNING就返回false。然后创建线程进行执行,状态修改。

代码语言:javascript代码运行次数:0运行复制
static void* Routine(void* args){     Thread* t=static_cast(args);     t->_status=STATUS::RUNNING;     t->_func(t->Name());     return nullptr;}bool Start(){     if(_status!=STATUS::RUNNING)     {           int n=::pthread_create(&_tid,nullptr,Routine,this);           if(n!=0)           {               //线程创建失败               return false;           }           return true;     }     //已经在RUNNING了     return false;}
3.3Stop函数:代码语言:javascript代码运行次数:0运行复制
bool Stop(){    //在运行的时候,才能cancle    if(_status==STATUS::RUNNING)    {        _status=STATUS::STOP;        int n=::pthread_cancel(_tid);        if(n!=0)        {            return true;        }    }    return true;}

3.4Join函数,EnableJoin函数:

代码语言:javascript代码运行次数:0运行复制
void EnableDetach(){//状态变为false_joinable = false;}bool Join(){//没有被分离才能joinif (_joinable){int n = ::pthread_join(_tid, nullptr);if (n != 0)return false;_status = STATUS::STOP;return true;}return false;}void Detach(){EnableDetach();//线程分离pthread_detach(_tid);}

4.整体代码:代码语言:javascript代码运行次数:0运行复制
#pragma once #include #include #include #include #include #include #include #include #include "Mutex.hpp"namespace ThreadModule{    using namespace MutexModule;    using func_t=std::function;    static int num=1;    enum class STATUS    {        RUNNING=1,        STOP,        NEW     //新的线程的状态    };    class Thread    {    private:        //执行方法,routine惯例        static void* Routine(void* args)        {            Thread* t=static_cast(args);            t->_status=STATUS::RUNNING;            t->_func(t->Name());            return nullptr;        }        void EnableDetach()        {            //状态变为false            _joinable=false;        }    public:        Thread(func_t func)            :_pid(getpid())             ,_func(func)             ,_joinable(true)             ,_status(STATUS::NEW)        {            LockGuard lockguard(_lock);            {                _name="thread-"+std::to_string(num++);              }        }        bool Start()        {            if(_status!=STATUS::RUNNING)            {                int n=::pthread_create(&_tid,nullptr,Routine,this);                if(n!=0)                {                    //线程创建失败                    return false;                }                return true;            }            //已经在RUNNING了            return false;        }        bool Stop()        {            //在运行的时候,才能cancle            if(_status==STATUS::RUNNING)            {                _status=STATUS::STOP;                int n=::pthread_cancel(_tid);                if(n!=0)                {                    return false;                }                return true;            }            return true;        }        bool Join()        {            //没有被分离才能join            if(_joinable)            {                int n=::pthread_join(_tid,nullptr);                if(n!=0)                    return false;                _status=STATUS::STOP;                return true;            }            return false;        }                void Detach()        {            EnableDetach();            //线程分离            pthread_detach(_tid);        }        std::string Name()  {return _name;}        bool JoinAble(){return _joinable;}        ~Thread()        {        }    private:        std::string _name;  //线程名字        pthread_t _tid;     //线程tid        pid_t _pid;         //线程属于哪个进程pid                STATUS _status;     //线程的状态        bool _joinable;     //是否被分离        func_t _func;       //线程执行的方法        Mutex _lock;        //锁进行保护    };}

Start

Stop

Join

EnableJoin

Name

JoinAble

标签:# 线程  # 值为  # 很方便  # 中有  # 两种  # 要有  # 还没有  # 这是  # 的是  # 访问权限  # 几个  # https  # this  # 对象  # Thread  # linux  # 指针  # void  # int  # bool  # 析构函数  # 构造函数  # 成员变量  # 封装  # String  # Static  # html  # JavaScript  # 为什么  
在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!