C++ 11 深度学习(二十一)C++线程封装

#pragma once
#include<thread>

class BaseThread
{
public:
    
    void Start();

    void Wait();

    void Stop();

    bool is_exit();

private:
    virtual void Run() = 0;

private:
    std::thread thread_;
    bool is_exit_ = false;

};
#include "BaseThread.h"

void BaseThread::Start()
{
	thread_ = std::thread(&BaseThread::Run, this);
}

void BaseThread::Wait()
{
	if (thread_.joinable())
	{
		thread_.join();
	}
}

void BaseThread::Stop()
{
	is_exit_ = true;
	Wait();
}

bool BaseThread::is_exit()
{
	return is_exit_;
}
#include<iostream>
#include"BaseThread.h"

class XThread:public BaseThread
{
public:
	void Run()
	{
		std::cout << "hello world" << std::endl;
	}
};




int main()
{

	XThread test;
	test.Start();
	test.Wait();
	std::cout << "This is main" << std::endl;
	system("pause");
	return 0;
}