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;
}
相关推荐
L***B5683 分钟前
如何安装linux版本的node.js
linux·运维·node.js
LCG元18 分钟前
终极武器:用 systemd 管理你的自定义应用服务(附配置文件)
linux
闻缺陷则喜何志丹39 分钟前
【SOSDP模板 容斥原理 逆向思考】3757. 有效子序列的数量|分数未知
c++·算法·力扣·容斥原理·sosdp·逆向思考
CoovallyAIHub42 分钟前
如何在手机上轻松识别多种鸟类?我们发现了更简单的秘密……
深度学习·算法·计算机视觉
第二只羽毛1 小时前
遵守robots协议的友好爬虫
大数据·爬虫·python·算法·网络爬虫
脏脏a1 小时前
【Linux】Linux进程状态深度解析
linux·运维·服务器
凉晓风1 小时前
Linux中常见几种自启动方式的区别
linux·运维·服务器
BestOrNothing_20151 小时前
一篇搞懂 C++ 重载:函数重载 + 运算符重载,从入门到会用(含 ++、<<、== 实战)
c++·函数重载·运算符重载·operator·前置后置++·重载与重写
LCG元1 小时前
考古利器:find 命令的高级用法,按时间、大小、内容精准查找
linux
艾斯比的日常1 小时前
Java 三色标记算法:并发垃圾回收的核心技术解析
java·开发语言·算法