# Multithreading a Custom Landscape

**URL:** <https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852>\
**Category:** Programming & Scripting\
**Tags:** threading, multithreading, unreal-engine, question, CPP\
**Created:** [September 10, 2024, 3:26pm UTC](https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852 "2024-09-10T15:26:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aMustachedPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/amustachedpotato/32/953940_2.png) [@aMustachedPotato](https://forums.unrealengine.com/u/aMustachedPotato)\
**Post date:** [September 10, 2024, 3:26pm UTC](https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852/1 "2024-09-10T15:26:02Z")

</div>

**The Problem**  
I’m creating a landscape using an InstancedStaticMeshComponent using noise. The calculations were taking up too much of the game thread, so I decided to put it on a separate thread. The problem seems to be that the task executes too fast and leaves gaps in my landscape.

 ![NoSleep](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/b/9/2b978505c00defb96b0c824d4d1cfcfd54b14ad9.jpeg) The problem is fixed if I use an `FPlatformProcess::Sleep(0.01f);` but that takes forever to generate my landscape and also has the same issue if I switch windows and the framerate drops.  
 ![SleepThenSwitchWindow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/9/d/79d1c8003ee74e02aee2533b87010e09eb43c9b5.jpeg)  
 ![SleepWindowFocused](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/e/f/6efeb465438fe768ae859e4202a85cd468dfcc34.jpeg)

**The Code**  
This is the shortened version of the class responsible for creating the landscape

```auto
class FAsyncWorldCreation : public FNonAbandonableTask
{
	friend class FAutoDeleteAsyncTask<FAsyncWorldCreation>;
public:

	DECLARE_DELEGATE_TwoParams(FAsyncGenerationCompleted, FAsyncPartitionGrid, FAsyncTileReturn)
	DECLARE_DELEGATE_TwoParams(FTileReturnCall, FTransform, int32)

	UPROPERTY()
	FAsyncGenerationCompleted OnAsyncCreateComplete;

	UPROPERTY()
	FTileReturnCall TestTileReturn;

	FAsyncWorldCreation(AWorldGenerator* InWorld, bool bNoTrees, bool bNoSeed);

	void DoWork();
	FORCEINLINE TStatId GetStatId() const { // Probably declares the Task to the TaskGraph
		RETURN_QUICK_DECLARE_CYCLE_STAT(FAsyncWorldCreation, STATGROUP_ThreadPoolAsyncTasks);
	}

private:

	UPROPERTY()
	TWeakObjectPtr<AWorldGenerator> WorldRef;

	FAsyncTileReturn AsyncGenerateWorld(FAsyncPartitionGrid& ReturnWorld);

};

```

And this is how I create the new Task

```auto
	AsyncTask(ENamedThreads::AnyHiPriThreadHiPriTask, [&]() 
		{
			(new FAutoDeleteAsyncTask<FAsyncWorldCreation>(this, bNoTrees, bDontGenerateSeed))->StartBackgroundTask();
		});

```

To generate the landscape I nest the Y loop in the X loop, and for every Y tile created I execute a delegate that adds an instance to the component. I have very little knowledge with multithreading, so I have no idea how to solve this.

**The Question**  
How can I do the calculations on a separate thread without slowing the thread down and still have the landscape generate properly.

EDIT  
After some more testing, it seems to be something with the way the delegate is executed. If I execute this after every Y tile

```auto
 AsyncTask(ENamedThreads::GameThread, [&]()
					{
						TestTileReturn.ExecuteIfBound(InstanceTransform, LandType);
						//UE_LOG(LogTemp, Warning, TEXT("X: %d, Y: %d"), xi, yi)
					});

```

it generates the whole world with holes in it. However, if I change the `&` to a `=` It doesn’t generate wholes, but it also doesn’t generate the whole world.

---

<div class="post-metadata">

**Author:** ![gardian206](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@gardian206](https://forums.unrealengine.com/u/gardian206)\
**Post date:** [September 10, 2024, 4:29pm UTC](https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852/2 "2024-09-10T16:29:15Z")

</div>

is this a thread per “pillar” or a thread per region, you will also be making a big assumption on the number of cores/threads the user will have. Take a look at the [Steam Hardware survey](https://store.steampowered.com/hwsurvey/Steam-Hardware-Software-Survey-Welcome-to-Steam) for core count, I can’t find the tread count, and we can not presume Hyper Threading/ SMT (because of different actions taken by Intel and AMD over the years)

maybe expanding the physical size and going into bigger “chunks” can “slow it down” it can also lead to more interesting deviations (especially if there is some random offset into the noise function for a given chunk).

be aware that there is a cost of spinning up the Async in the first place (segmenting memory which could hit paging with big operations or “many” small operations), and then a cost in the main-thread being paused on the completion of the Async. Then anything dealing with “spawn” happens on the main thread in the first place.

---

<div class="post-metadata">

**Author:** ![aMustachedPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/amustachedpotato/32/953940_2.png) [@aMustachedPotato](https://forums.unrealengine.com/u/aMustachedPotato)\
**Post date:** [September 10, 2024, 5:17pm UTC](https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852/3 "2024-09-10T17:17:07Z")

</div>

There is one background thread for the whole world. I have optimized the calculations so it takes about 1 - 3 seconds on my intel i5. I am aware that there will be a hitch once the instances are created, but I would like to have the loading screen not freeze up for the entire time the world is being generated (especially for slower devices). As for memory I have two structs. One which holds a `TArray<FBox>` and a `TArray<FPartitionedTiles>` (`FPartitionedTiles` is just an array for holding tile info) and then the other for holding the tile Transforms and material switch (`TArray<FTransform>` and a `TArray<int32>`)

---

<div class="post-metadata">

**Author:** ![aMustachedPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/amustachedpotato/32/953940_2.png) [@aMustachedPotato](https://forums.unrealengine.com/u/aMustachedPotato)\
**Post date:** [September 12, 2024, 3:02pm UTC](https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852/4 "2024-09-12T15:02:38Z")

</div>

Anyone know how to solve this?

---

<div class="post-metadata">

**Author:** ![aMustachedPotato](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/amustachedpotato/32/953940_2.png) [@aMustachedPotato](https://forums.unrealengine.com/u/aMustachedPotato)\
**Post date:** [September 22, 2024, 8:46pm UTC](https://forums.unrealengine.com/t/multithreading-a-custom-landscape/2014852/5 "2024-09-22T20:46:42Z")

</div>

Can anyone show me how to run a thread in the background just like it would on the game thread?
