Linux---线程封装

threat.hpp

cpp 复制代码
#ifndef _THREAD_HPP_
#define _THREAD_HPP_
#include <cstring>
#include <iostream>
#include <pthread.h>
#include <string>
#include <cstring>
#include <cstdio>
#include<functional>

namespace Z {

    static int number = 1;
    class thread {
        using func_t = std::function<void()>;
    private:
        void EnableDetach() {
            std::cout << "线程被分离了" << std::endl;
            _isdetach = true;
        }
        void EnableRunning() {
            std::cout << "线程启动" << std::endl;
            _isrunning = true;
        }

        static void *routine(void *args) {
            thread *self = static_cast<thread *>(args);
            self->EnableRunning();
            if(self->_isdetach)
                self->Detach();
            pthread_setname_np(self->_tid,self->_name.c_str());
            self->_func();
            return nullptr;
        }

    public:
        thread(func_t func) : _tid(0), _isdetach(false), _isrunning(false), _ret(nullptr), _func(func)
        {
            _name = "thread-"+std::to_string(number++);
        }

        void Detach() {
            if (_isdetach)
                return;
            if (_isrunning)
                pthread_detach(_tid);
            EnableDetach();
        }

        bool Start() {
            if (_isrunning)
                return false;
            int p = pthread_create(&_tid, nullptr, routine, this);
            if (p != 0) {
                std::cerr << "create threat fail!" << std::endl;
                return false;
            } else {
                {
                    std::cout << _name << "create success" << std::endl;
                    return true;
                }
            }
        }

        void Join() {
            if (_isdetach) {
                std::cout << "线程已分离,无法join" << std::endl;
                return;
            }
            int n = pthread_join(_tid, &_ret);
            if (n != 0) {
                std::cerr << "join 失败了" << strerror(n) << std::endl;
            } else {
                std::cout << "join success" << std::endl;
            }
        }

        bool Stop() {
            if (_isrunning) {
                int n = pthread_cancel(_tid);
                if (n != 0) {
                    std::cerr << "create thread error: " << strerror(n) << std::endl;
                    return false;
                } else {
                    _isrunning = false;
                    std::cout << _name << "stop" << std::endl;
                    return true;
                }
            }
            return true;
        }
        ~thread() {
        }

    private:
        pthread_t _tid;
        std::string _name;
        bool _isdetach;
        bool _isrunning;
        void *_ret;
        func_t _func;
    };

} 

#endif

test.cc

cpp 复制代码
#include "thread.hpp"
#include <unistd.h>

using namespace Z;
int main() {
    std::vector<thread> threads;
    for (int i = 0; i < 10; i++) {
        threads.emplace_back([]() {
            while (true) {
                char name[128];
                pthread_getname_np(pthread_self(), name, sizeof(name));
                std::cout << "我是一个新线程" << "name: " << name << std::endl;
                sleep(1);
            }
        });
    }

    for(auto &thread:threads)
    {
        thread.Start();
    }
    for(auto &thread:threads)
    {
        thread.Join();
    }

    return 0;
}
相关推荐
rainbow688924 分钟前
EffectiveC++入门:四大习惯提升代码质量
c++
秋邱40 分钟前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python
我在人间贩卖青春1 小时前
C++之析构函数
c++·析构函数
野犬寒鸦1 小时前
从零起步学习并发编程 || 第六章:ReentrantLock与synchronized 的辨析及运用
java·服务器·数据库·后端·学习·算法
霖霖总总1 小时前
[小技巧66]当自增主键耗尽:MySQL 主键溢出问题深度解析与雪花算法替代方案
mysql·算法
HalvmånEver1 小时前
Linux:线程互斥
java·linux·运维
rainbow68891 小时前
深入解析C++STL:map与set底层奥秘
java·数据结构·算法
番茄灭世神1 小时前
Linux应用编程介绍
linux·嵌入式
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [drivers][mmc][mmc_sdio]
linux·笔记·学习
我在人间贩卖青春1 小时前
C++之数据类型的扩展
c++·字符串·数据类型