linux学习笔记: https://blog.csdn.net/djdjiejsn/category_12669243.html
前言:
下面关于锁的封装看看下面的:Lockguard的构造函数加锁,析构函数解锁,用起来很方便。
代码语言:javascript代码运行次数:0运行复制<code class="javascript">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; };}</code>1.类中的static函数为什么不能直接访问内部成员变量?
因为默认没有传this指针。所以static函数参数中有this指针的时候,还是可以访问的,此时this(对象)指针不能省略。
2.外部函数不能访问私有成员?
没有默认传递this指针,没有访问权限。声明是类的友元函数的时候,也是可以访问的。
代码:
代码语言:javascript代码运行次数:0运行复制<code class="javascript">#include <iostream>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;}</code>
表示线程的状态,NEW表示新的线程,还没有进行Start操作时的状态信息。
2.4.是否被分离:线程默认是没有被分离的,而且只有分离和没有被分离两种情况,bool类型。
代码语言:javascript代码运行次数:0运行复制<code class="javascript">void EnableDetach(){ //状态变为false,新的线程默认为true _joinable=false;}void Detach(){ EnableDetach(); //线程分离 pthread_detach(_tid);}</code>为了区分不同的线程可以给线程取个名字。
名字可以取"thread-num"表示是第几个线程。
2.6.执行的方法:这里定义的是void(std::string name),返回值为void,参数是string的函数。

_name:
线程名字以"thread-num"来命名,表示第几个线程。所以定义一个static int num进行记录有多少个线程。但是这是临界资源,线程再创建线程,导致方式混乱。
_joinable:
_joinable默认是没有被分离的,所以是true。
_func:
_func是外部传进来要执行的方法。所以要有func_t 参数。
_status:
为新线程,状态为NEW。
_tid:
在线程创建的时候,传到pthread里面进行确定。
代码语言:javascript代码运行次数:0运行复制<code class="javascript">Thread(func_t func) :_pid(getpid()) ,_func(func) ,_joinable(true) ,_status(STATUS::NEW){ LockGuard lockguard(_lock); { _name="thread-"+std::to_string(num++); }}</code>
先判断是不是没有在RUNNING,如果在RUNNING就返回false。然后创建线程进行执行,状态修改。
代码语言:javascript代码运行次数:0运行复制<code class="javascript">static void* Routine(void* args){ Thread* t=static_cast<Thread*>(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;}</code><code class="javascript">bool Stop(){ //在运行的时候,才能cancle if(_status==STATUS::RUNNING) { _status=STATUS::STOP; int n=::pthread_cancel(_tid); if(n!=0) { return true; } } return true;}</code>3.4Join函数,EnableJoin函数:
代码语言:javascript代码运行次数:0运行复制<code class="javascript">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);}</code><code class="javascript">#pragma once #include <iostream>#include <cstdio>#include <string.h>#include <cstring>#include <pthread.h>#include <functional>#include <unistd.h>#include <sys/types.h>#include "Mutex.hpp"namespace ThreadModule{ using namespace MutexModule; using func_t=std::function<void(std::string name)>; static int num=1; enum class STATUS { RUNNING=1, STOP, NEW //新的线程的状态 }; class Thread { private: //执行方法,routine惯例 static void* Routine(void* args) { Thread* t=static_cast<Thread*>(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; //锁进行保护 };}</code>Start
Stop
Join
EnableJoin
Name
JoinAble
以上就是【Linux课程学习】:锁封装(Mutex)线程封装(Thread),this指针的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号