Skip to content

Instantly share code, notes, and snippets.

@the-nerdery-dot-info
Created October 29, 2016 21:42
Show Gist options
  • Save the-nerdery-dot-info/bfdd385975c3c60002eb11692d2bb48e to your computer and use it in GitHub Desktop.
Save the-nerdery-dot-info/bfdd385975c3c60002eb11692d2bb48e to your computer and use it in GitHub Desktop.
how to accomplish cpu multithreading in unreal engine
UCLASS(config=Game)
class AMultiThreadingCharacter : public ACharacter
{
GENERATED_BODY()
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
public:
AMultiThreadingCharacter();
/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseTurnRate;
/** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseLookUpRate;
protected:
/** Called for forwards/backward input */
void MoveForward(float Value);
/** Called for side to side input */
void MoveRight(float Value);
/**
* Called via input to turn at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void TurnAtRate(float Rate);
/**
* Called via input to turn look up/down at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void LookUpAtRate(float Rate);
/** Handler for when a touch input begins. */
void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);
/** Handler for when a touch input stops. */
void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);
protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
// End of APawn interface
public:
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
protected:
/*Calculates prime numbers in the game thread*/
UFUNCTION(BlueprintCallable, Category = MultiThreading)
void CalculatePrimeNumbers();
/*Calculates prime numbers in a background thread*/
UFUNCTION(BlueprintCallable, Category = MultiThreading)
void CalculatePrimeNumbersAsync();
/*The max prime number*/
UPROPERTY(EditAnywhere, Category = MultiThreading)
int32 MaxPrime;
};
namespace ThreadingTest
{
static void CalculatePrimeNumbers(int32 UpperLimit)
{
//Calculating the prime numbers...
for (int32 i = 1; i <= UpperLimit; i++)
{
bool isPrime = true;
for (int32 j = 2; j <= i / 2; j++)
{
if (FMath::Fmod(i, j) == 0)
{
isPrime = false;
break;
}
}
if (isPrime) GLog->Log("Prime number #" + FString::FromInt(i) + ": " + FString::FromInt(i));
}
}
}
/*PrimeCalculateAsyncTask is the name of our task
FNonAbandonableTask is the name of the class I've located from the source code of the engine*/
class PrimeCalculationAsyncTask : public FNonAbandonableTask
{
int32 MaxPrime;
public:
/*Default constructor*/
PrimeCalculationAsyncTask(int32 MaxPrime)
{
this->MaxPrime = MaxPrime;
}
/*This function is needed from the API of the engine.
My guess is that it provides necessary information
about the thread that we occupy and the progress of our task*/
FORCEINLINE TStatId GetStatId() const
{
RETURN_QUICK_DECLARE_CYCLE_STAT(PrimeCalculationAsyncTask, STATGROUP_ThreadPoolAsyncTasks);
}
/*This function is executed when we tell our task to execute*/
void DoWork()
{
ThreadingTest::CalculatePrimeNumbers(MaxPrime);
GLog->Log("--------------------------------------------------------------------");
GLog->Log("End of prime numbers calculation on background thread");
GLog->Log("--------------------------------------------------------------------");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment