UE5 多线程(小白自学记录,自我理解最简单多线实现)

C++接口

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "MyInterface.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class YIBUJIAZHAITEST_API IMyInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

	virtual void dowork(){}
};

线程.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Misc/Fork.h"
#include "MyInterface.h"//包含接口头文件

/**
 * 
 */
class YIBUJIAZHAITEST_API ThreadTask :public FRunnable
{
public:
	ThreadTask();

	virtual uint32 Run();// 1、

	void CreateThread(IMyInterface* newmyinterface);// 3、创建线程调用(2)
	//void CreateThread(以接口的方式传递指针,执行接口类的新成员)

	~ThreadTask();
	 
private:
	IMyInterface* MyInterface;// 4、创建接口实例

	FRunnableThread* Task_Thread;// 2、创建线程实例
};

线程.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "ThreadTask.h"

ThreadTask::ThreadTask()
{
	MyInterface = nullptr;//初始化接口为空
}

uint32 ThreadTask::Run()
{
	UE_LOG(LogTemp, Log, TEXT("Run"));

	if (MyInterface)// 6、传递成功
	{
		MyInterface->dowork();// 7、执行dowork() ,打开gamemodebase.h创建具体任务
	}
		
	return 0;//到这里线程结束
}

void ThreadTask::CreateThread(IMyInterface* newmyinterface)
//CreateThread(IMyInterface* newmyinterface)创建时需要传递这个成员,凡是继承这个接口的都可以执行dowork
{
	MyInterface = newmyinterface;// 5、传递过来后把他放到Run里面执行

	//调用(2),线程创建成功会运行Run
	Task_Thread = FRunnableThread::Create(this, TEXT("HelloWorld"), 0, TPri_Normal);
}

ThreadTask::~ThreadTask()
{
}

gamemode.h

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "MyInterface.h"
#include "ThreadTask.h"
#include "GameFramework/Pawn.h"
#include "yibujiazhaiTestGameModeBase.generated.h"



/**
 * 
 */
UCLASS()
class YIBUJIAZHAITEST_API AyibujiazhaiTestGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

protected:
	virtual void BeginPlay() override;

	// 11、释放线程
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason);

private:
	IMyInterface* Inerface ;
	ThreadTask* newtask ;
	
};

gamemode.cpp

// Copyright Epic Games, Inc. All Rights Reserved.


#include "yibujiazhaiTestGameModeBase.h"

void AyibujiazhaiTestGameModeBase::BeginPlay()
{
	Super::BeginPlay();

	Inerface = new IMyInterface(); // 8、创建IMyInterface接口
	newtask = new ThreadTask(); // 9、创建 ThreadTask线程
	
	newtask->CreateThread(Inerface); // 10、创建一个线程将Inerface传进去,会执行dowork接口

}

void AyibujiazhaiTestGameModeBase::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);

	delete newtask;// 结束删除
	Inerface = nullptr; //接口置空
}

-------------------------------------------------华丽分割线-----------------------------------------------------------

以下是小白精简的最简单多线程实例,以便理解

一个actor类(目的是创建一个线程)

// Fill out your copyright notice in the Description page of Project Settings.

//this actor.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"

UCLASS()
class ZIJICS_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	int32 num;//一个变量

	UPROPERTY(EditAnywhere)
		int32 numtarget;//编辑器中可设置
};
// Fill out your copyright notice in the Description page of Project Settings.

//this is actor.cpp

#include "MyActor.h"
#include "MyThread.h"
#include "Kismet/KismetSystemLibrary.h"


// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	num = 0;

}

// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();

	//创建一个线程类的实例
	MyThread* runable1 = new MyThread();
	
	//创建一个线程
	FRunnableThread* runthread1 = FRunnableThread::Create(runable1, TEXT("这是线程1"));
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}


一个继承自FRunnable的OBJECT类

// Fill out your copyright notice in the Description page of Project Settings.

//this is Thread.h

#pragma once

#include "CoreMinimal.h"
#include "HAL/Runnable.h"
#include "MyActor.h"

class ZIJICS_API MyThread :public FRunnable
{
public:
	MyThread();

	~MyThread();

public:
	
	virtual bool Init() override;
	virtual uint32 Run() override;
	virtual void Exit() override;

private:
	class AMyActor* myactor;
		
};
// Fill out your copyright notice in the Description page of Project Settings.

//this is Thread.cpp

#include "MyThread.h"

MyThread::MyThread()
{
	
}

MyThread::~MyThread()
{
}

bool MyThread::Init()
{
	return true;
}

uint32 MyThread::Run()
{
	for (int32 i = 0; i < 100000; i++)
	{
		UE_LOG(LogTemp, Warning, TEXT("Task iteration %d"), i);
	}
	return 0;
}

void MyThread::Exit()
{
	UE_LOG(LogTemp, Log, TEXT("this is end exit"));

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我救我自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值