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类:添加一个uobject类,删除#include "UObject/Object.h",UCLASS(),GENERATED_BODY()
class MEDIAPIPE4UDEMO_API UMyThreadClass : public UObject修改为
class MEDIAPIPE4UDEMO_API UMyThreadClass : public FRunnable
#include "HAL/Runnable.h"
// 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);
// 线程休眠5秒
//FPlatformProcess::Sleep(5.0f);
//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Magenta, TEXT("Thread is
//sleeping for 5.0 seconds"));
//}
}
return 0;
}
void MyThread::Exit()
{
UE_LOG(LogTemp, Log, TEXT("this is end exit"));
FRunnable::Exit();
}
actor中创建线程,任务完成后销毁线程:
-
在
MyThreadClass
中添加一个委托:// MyThreadClass.h #pragma once #include "CoreMinimal.h" #include "HAL/Runnable.h" #include "Delegates/Delegate.h" DECLARE_DELEGATE(FOnThreadFinished); class MYPROJECT_API MyThreadClass : public FRunnable { public: MyThreadClass(); virtual ~MyThreadClass(); virtual bool Init() override; virtual uint32 Run() override; virtual void Exit() override; void SetThreadFinishedDelegate(FOnThreadFinished InDelegate); private: FOnThreadFinished ThreadFinishedDelegate; };
// MyThreadClass.cpp #include "MyThreadClass.h" #include "MyObjectClass.h" #include "Engine/Engine.h" MyThreadClass::MyThreadClass() { } MyThreadClass::~MyThreadClass() { } bool MyThreadClass::Init() { return true; } uint32 MyThreadClass::Run() { FPlatformProcess::Sleep(5.0f); UMyObjectClass* ObjectClass = UMyObjectClass::GetInstance(); if (ObjectClass) { ObjectClass->bConditionTriggered = false; } // 通知线程已完成 if (ThreadFinishedDelegate.IsBound()) { ThreadFinishedDelegate.Execute(); } return 0; } void MyThreadClass::Exit() { FRunnable::Exit(); } void MyThreadClass::SetThreadFinishedDelegate(FOnThreadFinished InDelegate) { ThreadFinishedDelegate = InDelegate; }
-
在
ATestActor
中管理线程的生命周期:// TestActor.h #pragma once #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyThreadClass.h" #include "TestActor.generated.h" UCLASS() class MYPROJECT_API ATestActor : public AActor { GENERATED_BODY() public: ATestActor(); virtual ~ATestActor(); virtual void BeginPlay() override; virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override; void Callback(); void OnThreadFinished(); private: FRunnableThread* RunnableThread1; MyThreadClass* Runnable1; };
// TestActor.cpp #include "TestActor.h" #include "Engine/Engine.h" ATestActor::ATestActor() { PrimaryActorTick.bCanEverTick = true; } ATestActor::~ATestActor() { // 清除线程 if (RunnableThread1) { RunnableThread1->Kill(true); // true表示等待线程完成 delete RunnableThread1; RunnableThread1 = nullptr; } // 清除可运行对象 if (Runnable1) { delete Runnable1; Runnable1 = nullptr; } } void ATestActor::BeginPlay() { Super::BeginPlay(); bHasTriggered = true; MyObjectClass = UMyObjectClass::GetInstance(); if (MyObjectClass) { MyObjectClass->Tiao.AddUObject(this, &ATestActor::Callback); } testmesh->SetSimulatePhysics(true); } void ATestActor::EndPlay(const EEndPlayReason::Type EndPlayReason) { Super::EndPlay(EndPlayReason); // 清除线程 if (RunnableThread1) { RunnableThread1->Kill(true); // true表示等待线程完成 delete RunnableThread1; RunnableThread1 = nullptr; } // 清除可运行对象 if (Runnable1) { delete Runnable1; Runnable1 = nullptr; } } void ATestActor::Callback() { testmesh->AddImpulse(FVector(0.0f, 0.0f, 50000.0f)); Runnable1 = new MyThreadClass(); Runnable1->SetThreadFinishedDelegate(FOnThreadFinished::CreateUObject(this, &ATestActor::OnThreadFinished)); RunnableThread1 = FRunnableThread::Create(Runnable1, TEXT("这是线程1")); // 等待线程完成,这是阻塞主线程的方法 // if (RunnableThread1) // { // RunnableThread1->WaitForCompletion(); // delete RunnableThread1; // RunnableThread1 = nullptr; // } } void ATestActor::OnThreadFinished() { // 清除线程 if (RunnableThread1) { RunnableThread1->Kill(true); // true表示等待线程完成 delete RunnableThread1; RunnableThread1 = nullptr; } // 清除可运行对象 if (Runnable1) { delete Runnable1; Runnable1 = nullptr; } }