zoukankan      html  css  js  c++  java
  • 面向对象风格的线程类封装

    本程序使用面向对象的风格对线程进行类封装,代码如下:
    类头文件:

    //start from the very beginning,and to create greatness
    //@author: Chuangwei Lin
    //@E-mail:979951191@qq.com
    //@brief: 面向对象风格的线程类封装
    #include <pthread.h>
    #include <iostream>
    
    #ifndef _LCW_THREAD_H_
    #define _LCW_THREAD_H_
    //线程基类
    class lcw_thread
    {
    public:
        lcw_thread();
        virtual ~lcw_thread();//虚析构函数
        void Start();
        void Join();
        void SetAutoDelete(bool AutoDelete);
    private:
        static void* Thread_Routine(void* arg);
        virtual void Run() = 0;//Run隐含的第一个参数是this指针,纯虚函数,基类不实现,为抽象接口
        pthread_t lcwid;    
        bool AutoDelete;//是否自动销毁
    };
    #endif

    类实现:

    //start from the very beginning,and to create greatness
    //@author: Chuangwei Lin
    //@E-mail:979951191@qq.com
    //@brief: 面向对象风格的线程类封装
    #include "lcw_thread.h"
    using namespace std;
    lcw_thread::lcw_thread() :AutoDelete(false)
    {
        cout << "基类构造函数lcw_thread" << endl;
    }
    lcw_thread::~lcw_thread()
    {
        cout << "基类析构函数~lcw_thread" << endl;
    }
    void lcw_thread::Start()//启动线程
    {
        pthread_create(&lcwid,NULL,Thread_Routine,this);//this指针是参数
    }
    void lcw_thread::Join()
    {//函数pthread_join用来等待一个线程的结束
        pthread_join(lcwid,NULL);
    }
    void* lcw_thread::Thread_Routine(void* arg)
    {//参数arg其实是this指针
        lcw_thread* lcwthread = static_cast<lcw_thread*>(arg);//指向派生类指针转化为基类对象指针
        lcwthread->Run();
        if(lcwthread->AutoDelete)//
        {
            delete lcwthread;//销毁线程
        }
        return NULL;
    }
    void lcw_thread::SetAutoDelete(bool AutoDelete_)  
    {
        AutoDelete = AutoDelete_;//设置是否自动销毁
    }

    派生类和主测试函数:

    //start from the very beginning,and to create greatness
    //@author: Chuangwei Lin
    //@E-mail:979951191@qq.com
    //@brief: 面向对象风格的线程类封装
    #include "lcw_thread.h"
    #include <unistd.h>
    
    using namespace std;
    //派生类,从lcw_thread共有继承
    class lcw_thread_test : public lcw_thread
    {
    public:           //count_初始化为count
        lcw_thread_test(int count):count_(count)
        {
            cout << "派生类构造函数lcw_thread_test" << endl;
        }
        ~lcw_thread_test()
        {
            cout << "派生类析构函数~lcw_thread_test" <<endl;
        }
        void Run()
        {
            while(count_--)
            {
                cout <<"子线程第"<< 5-count_ <<"次测试" << endl;
                sleep(1);
            }
        }
        int count_;
    };
    int main(int argc, char const *argv[])
    {
        // lcw_thread_test lcw(5);//创建派生类对象
        // lcw.Start();//创建线程
        //以下动态创建对象,使得线程周期结束时线程对象能够自动销毁
        lcw_thread_test* lcw = new lcw_thread_test(5);
        lcw->SetAutoDelete(true);//设置自动销毁
        lcw->Start();
    
        //主函数也来下输出
        for(int i =1 ;i <= 5;++ i)
        {
            cout << "主函数第"<< i <<"次输出" << endl;
            sleep(1);
        }
        //lcw->Join();//等待线程结束
        return 0;
    }

    运行结果:
    这里写图片描述
    由于竞争关系,输出会有一点混乱

  • 相关阅读:
    ZOJ-3725 Painting Storages DP
    ZOJ-3720 Magnet Darts 计算几何,概率
    ZOJ-3721 Final Exam Arrangement 贪心
    POJ-2096 Collecting Bugs 概率DP
    [转]数据输入加速
    POJ-3468 A Simple Problem with Integers Splay Tree区间练习
    HDU-4419 Colourful Rectangle 矩形多面积并
    POJ-1177 Picture 矩形覆盖周长并
    HDU-1255 覆盖的面积 覆盖的矩形面积并
    POJ-1151 Atlantis 矩形面积并
  • 原文地址:https://www.cnblogs.com/sigma0-/p/12630493.html
Copyright © 2011-2022 走看看