zoukankan      html  css  js  c++  java
  • 面向对象与基于对象 学习记录 thread举例

    /********************************************************************/
    * @file
    * @author def< qq group: 324164944 >
    * @blog http://www.cnblogs.com/itdef/
    * @brief
    /********************************************************************/

    /*******************************************************************************
    * @file
    * @author def< qq group: 324164944 >
    * @blog  http://www.cnblogs.com/itdef/ 
    * @brief
    /*******************************************************************************/
    
    #include "stdafx.h"
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    class CThread{
    public:
    	CThread();
    	virtual ~CThread();
    	bool Start();
    	void Join();
    	static DWORD WINAPI ThreadProc( LPVOID lpParameter);
    	virtual void Run() = 0;
    private:	
    	HANDLE	hThread_;
    	DWORD	dwThreadId_;
    };
    
    CThread::CThread():
    hThread_(NULL),dwThreadId_(0)
    {
    	cout << "Thread ..." << endl;
    }
    
    CThread::~CThread()
    {
    	if(hThread_ != NULL)
    		CloseHandle(hThread_);
    	cout << "~Thread ..." << endl;
    }
    
    bool CThread::Start()
    {
    	bool bRet = false;
    	hThread_ = CreateThread( 
    		NULL,              // default security attributes
    		0,                 // use default stack size  
    		ThreadProc,          // thread function 
    		this,             // argument to thread function 
    		0,                 // use default creation flags 
    		&dwThreadId_);   // returns the thread identifier 
    
    	if(hThread_)
    	{
    		bRet = true;
    	}
    	return bRet;
    }
    
    void CThread::Join()
    {
    	WaitForSingleObject(hThread_,3000);
    }
    
    DWORD CThread::ThreadProc( LPVOID lpParameter)
    {
    	CThread* thread = static_cast<CThread*>(lpParameter);
    	thread->Run();
    	return NULL;
    }
    
    class CMyThread:public CThread
    {
    public:
    	void Run(){ cout << "my thread..." << endl;}
    };
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      CMyThread thread;
      thread.Start();  
      thread.Join(); 
      return 0; 
    }
    

      

    基类是最基本的几个元素 线程ID 创建进程的函数start 运行指定的线程函数run  以及等待函数join()

    使用的时候直接继承 在run函数中执行自己想执行的线程处理即可。

    基于对象则未使用继承等特性,使用bind function这对利器 来实现回调

    #include <windows.h>
    #include <iostream>
    #include <boost/function.hpp>
    
    class CThread
    {
    public:
    	typedef boost::function<void ()> ThreadFunc;
    	explicit CThread(const ThreadFunc& func);
    	~CThread();
    
    	void Start();
    	void Join();
    private:
    	static DWORD WINAPI ThreadProc(LPVOID arg);
    	void Run();
    	ThreadFunc func_;
    	HANDLE hThread_;
    
    };
    
    void CThread::Start()
    {
    	hThread_ = CreateThread(
    		NULL,              // default security attributes
    		0,                 // use default stack size 
    		ThreadProc,          // thread function
    		this,             // argument to thread function
    		0,                 // use default creation flags
    		NULL);   // returns the thread identifier
    }
    
    
    CThread::CThread(const ThreadFunc& func):
    hThread_(NULL),func_(func)
    {
    	std::cout << "CThread()..." << std::endl;
    }
    
    void CThread::Join()
    {
    	WaitForSingleObject(hThread_,3000);
    }
    
    CThread::~CThread()
    {
    	if(hThread_)
    		CloseHandle(hThread_);
    	std::cout << "~CThread()..." << std::endl;
    }
    
    void CThread::Run()
    {
    	func_();
    }
    
    
    DWORD CThread::ThreadProc(LPVOID arg)
    {
    	CThread* thread = static_cast<CThread*>(arg);
    	thread->Run();
    	return NULL;
    }
    
    
    //======================================
    
    void ThreadFunc()
    {
    	std::cout << "Enter thread function ...." << std::endl;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	CThread thread(ThreadFunc);
    	thread.Start();
    	thread.Join();
    	return 0;
    }
    

      

  • 相关阅读:
    oracle 7月份更新CVE-2020-14645 T3反序列化 Weblogic12.2.1.4.0 JNDI注入 Payload 复现&利用
    oracle 7月份更新 CVE-2020-14625 复现&利用
    Citrix Systems产品安全漏洞 CVE-2020-8193, CVE-2020-8195 and CVE-2020-8196 poc
    cve-2020-5902 RCE的payload以及绕过方式
    cve-2020-5902 BIG-IP RCE漏洞复现&exp
    Tomcat基于Servlet的无文件webshell的相关技术研究
    JBOSS 无文件webshell的技术研究
    weblogic 无文件webshell的技术研究
    java 获取包下的类 find all classes in a package
    冰蝎改造之适配基于tomcat Filter的无文件webshell
  • 原文地址:https://www.cnblogs.com/itdef/p/4375529.html
Copyright © 2011-2022 走看看