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"));
}