Terrain update as a thread

Hi,

I want to run geomipterrain’s update method on a separate thread, and preferably asynchronously (to get rid of the [tiny] lag spikes, especially for older computers and large terrains). This is the first time i’m working with threads, but it seemed relatively straightforward:

In the ‘TerrainManager’ class, which has a geomipterrain as attribute:

    
def updateTask(self,task):
        updateThread = threading.Thread(target=self.terrain.update)
        updateThread.start()
        return task.cont

I added this to the taskManader.

The result is that the program runs for about 5 seconds and then crashes without any error message. When i run Pstat the screen gets filled with ‘thread001, thread002…’, which is flickering (constantly refreshing).

It seemed like to many threads are created so I tried making the thread an attribute and call self.updateThread.start() every frame, but threads can only be run once (?). I also called del(updateThread) in the update task. Needless to say it didnt work. I am using the panda module for threading, (“from direct.stdpy import threading”).

Any ideas?
Thanks!

If you put it in a task, this means that you spawn a new thread every frame. After a few seconds, the number of threads you’ve created will be insanely high. Are you sure you don’t just want to create one thread?

You should implement an infinite loop in your thread, and use a lock to wait for a signal from the main thread to tell it to generate.

Alternatively, you could play with Panda’s task chains in order to create a task that runs on a separate thread.

aha thats how it works :slight_smile: strange that the garbage collector doesnt clear up all the unused threads then…

I added the following lines to init:

        taskMgr.setupTaskChain('terrainThread', numThreads = 1, frameSync = True)
        taskMgr.add(self.updateTask,"updateTerrain", taskChain = 'terrainThread')

and for self.updateTask:

        self.terrain.update()
        return task.cont

The terrain does update (noticable by wireframemode), and Pstat no longer spams all the threads, however the program still crashes in about 10 seconds. It still seems like i am creating infinit threads but i cant quite figure out why…

Are you sure you’re still creating infinite threads, or is it some different problem now? It sounds like you’ve solved the infinite-threads problem, but it might be crashing for some other reason. For instance, possibly GeoMipTerrain isn’t thread-safe, in which case it can’t be run safely in a thread because it might crash randomly.

In fact, it’s almost certain that GeoMipTerrain isn’t thread-safe, because of a current known problem with Panda’s geom structures which appear to be non-thread-safe when regenerating.

David

hmm, that would be a shame. Is there any way i can fix this; making two terrains and swapping them every update, update less frequently, build in some fail tests? Or is this already a planned update for an upcomming version of Panda perhaps?

Would really like to fix this: on my older laptop i’ve never gotten geomip to work properly due to the lag while updating.

+1

Would it be difficult to implement thread-safe geoms? Theres a few bugs in launchpad regarding threads, but not sure if they are related or not…

FYI - I never got geomipterrain updates running smoothly on my machine, so had to bake my terrain tiles to .bam files and then load them with LOD. A bit of a hack though, and results in some ugly seams in places, but its a smooth framerate.