架构Runnable多线程

Runnable.h

int main()
{
	char ThreadName[512] = "daxianyuThread_1";
	FMyRunnable(EThreadPriority::TPri_Normal, 0, ThreadName);
	Sleep(100000);

	return 0;
}

class FMyRunnable : public FRunnable
{
public:
	FMyRunnable(FRunnable* InRunable,
		const EThreadPriority InPriority /*= EThreadPriority::TPri_Normal*/,
		unsigned int InStack /*= 0*/,
		const char* InThreadName)
		:bStop(false)
	{	
		Thread = Platform::Create(this,InPriority, InStack, InThreadName);

	}

	virtual bool Init()
	{
		return true;
	};

	virtual int Run()
	{
		for (;;)
		{
			while (!bStop) //逻辑层
			{
				//具体逻辑
				Sleep(200);

				std::cout << "FMyRunnable" << std::endl;
			}

			Sleep(1000);
			break;
		}

		return 0;
	}

	virtual int Stop()
	{
		return 0;
	};

	virtual bool Exit()
	{
		delete Thread;
		return true;
	}

	virtual FRunnableThread* GetThread()
	{
		return Thread;
	}

protected:
	bool bStop;
	FRunnableThread* Thread;

};

class FRunnable
{
public:

	virtual bool Init() { return false; };

	virtual int Run() = 0;

	virtual int Stop() { return 0; };

	virtual bool Exit() { return false; };

	virtual FRunnableThread* GetThread() = 0;
};

RunnableThread.h

enum EThreadPriority
{
	TPri_Normal,
	TPri_AboveNormal,
	TPri_BelowNormal,
	TPri_Highest,
	TPri_Lowest,
	TPri_SlightlyBelowNormal,
	TPri_TimeCritical,
	TPri_Num,
};

class FRunnable;
class FRunnableThread
{
public:
	FRunnableThread();

	virtual bool Create(FRunnable* InRunable) = 0;

	void SetPriority(EThreadPriority InPriority);
	void SetThreadName(const char* InThreadName);
	void SetThreadID(unsigned int InThreadID);

	const EThreadPriority GetPriority()const;
	const char* GetThreadName() const;
	const unsigned int GetThreadID() const;

protected:
	EThreadPriority Priority;
	char ThreadName[512];
	unsigned int ThreadID;
};

RunnableThread.cpp

FRunnableThread::FRunnableThread()
	:Priority(EThreadPriority::TPri_Normal)
	, ThreadID(0u)
{
	memset(ThreadName, 0, 512);
}

void FRunnableThread::SetPriority(EThreadPriority InPriority)
{
	Priority = InPriority;
}

void FRunnableThread::SetThreadName(const char* InThreadName)
{
	memcpy(ThreadName, InThreadName, 512);
}

void FRunnableThread::SetThreadID(unsigned int InThreadID)
{
	ThreadID = InThreadID;
}

const EThreadPriority FRunnableThread::GetPriority() const
{
	return Priority;
}

const char* FRunnableThread::GetThreadName() const
{
	return ThreadName;
}

const unsigned int FRunnableThread::GetThreadID() const
{
	return ThreadID;
}

Platform.h

class Platform
{
public:
	static FRunnableThread* Create(
		FRunnable* InRunable,
		const EThreadPriority InPriority /*= EThreadPriority::TPri_Normal*/,
		unsigned int InStack /*= 0*/,
		const char* InThreadName = nullptr);
};

Platform.cpp

#if _linux
#include <pthread.h>
#endif

class FWin32RunnableThread : public FRunnableThread
{
public:

	virtual bool Create(FRunnable* InRunable);

protected:
	HANDLE h;
};

class FlinuxRunnableThread : public FRunnableThread
{
public:

	virtual bool Create(FRunnable* InRunable);
};

bool FWin32RunnableThread::Create(FRunnable* InRunable)
{
	if (InRunable)
	{
		auto RunThread = [](LPVOID lpPram)->DWORD
		{
			FRunnable* Runnable = (FRunnable*)lpPram; 

			FRunnableThread* MyThread = Runnable->GetThread();

			MyThread->SetThreadID(GetCurrentThreadId());

#if _WIN32
			
			struct FTagThreadNameInfo
			{
				DWORD dwType; //0x1000 4096
				LPCSTR szName;
				DWORD dwThreadID;
				DWORD dwFlags;
			};

			FTagThreadNameInfo Info;
			Info.dwType = 0x1000;
			Info.szName = MyThread->GetThreadName();
			Info.dwThreadID = MyThread->GetThreadID();
			Info.dwFlags = 0;
			__try
			{
				RaiseException(0x406D1388, 0, sizeof(Info) / sizeof(DWORD), (DWORD*)(&Info));
			}
			__except (EXCEPTION_CONTINUE_EXECUTION)
			{
				std::cout << "EXCEPTION_CONTINUE_EXECUTION" << std::endl;
			}		
#elif _linux
			//linux //修改名字
			
#endif
			DWORD ReturnValue = Runnable->Run();

			Runnable->Exit();

			return ReturnValue;
		};


		HANDLE h = CreateThread(nullptr, 0, RunThread, nullptr, 0, nullptr);
		return true;
	}
	return false;
}

FRunnableThread* Platform::Create(FRunnable* InRunable,
	const EThreadPriority InPriority /*= EThreadPriority::TPri_Normal*/,
	unsigned int InStack /*= 0*/,
	const char* InThreadName)
{
	FRunnableThread* InThread = nullptr;

	if(InRunable)
	{ 

#if _WIN32
		if(InRunable->Init())
		{ 
			InThread = new FWin32RunnableThread();

			//初始化
			InThread->SetPriority(InPriority);
			InThread->SetThreadName(InThreadName);

			//执行
			InThread->Create(InRunable);
		}

#elif _linux
		InThread = new FlinuxRunnableThread();

#endif
	}
	return InThread;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值