Starting threads from within a GL Thread safe?

would it be safe to start a new thread out of a gamestates update method?

I would like to start sending movement packets to server in a new Thread as soon as the player starts moving which gets triggered in a gamestates update method and stop / interrupting this thread when player stops again out of the gamestates update method

It will be safe as long as you properly synchronize access to the data you’re using. (Warning, rant ahead)

Option 1 - Single Thread

One solution that you can use is to send these movement packets inside of the GameState itself (call a method in the networking class).  I don’t need to send as quickly as possible (as that’s not always the best idea), so I have it set to send every X seconds.  You could, like you may want to do, use another thread to control the networking, however you may only need something as simple as your traditional update loop.

Here’s a simple example to show you what I mean:

public static final float UPDATE_EVERY = .250f; // Send a position update every 250 ms

private float timeUntilUpdate = UPDATE_EVERY;
public void update(float tpf)
    timeUntilUpdate -= tpf;
    if(timeUntilUpdate <= 0.0f)
        // Reset our "timer"
        timeUntilUpdate = UPDATE_EVERY;
    // Do everything else, too!

May want to substitute that if-statement with something more efficient, but you get the drift.  You'll want to make sure you're not making any blocking calls for networking in there, though.  If so...

Option 2 - Using Multiple Threads
Could have that "sendMovementUpdate" take a new, distinct copy of the player's data (note: use clone, don't do tempObject = oldObject) and pass that to a data structure that the networking thread will retrieve data from.  This data structure will have to be synchronized.  After adding it, you could notify the other thread, and then go along your business.

I don't know if you want to start the thread in the GameState or not, from there it's your choice - but be careful not to spawn one each update!

Thanks this info helped me alot!

By the way how did you make your chat box in objive (took a look at the screenshots :slight_smile: )? Did you use FengGUI or did you write it completely by yourself using quads and putting text on them?

Yeah, those screens that are up now were mainly tests of different functionality to build on.

I used GBUI.  The chat box is simply a BTextArea and BTextField in a BorderLayout, placed into a BWindow.  I had that plugged into my networking architecture and had chatting going on, but that screenshot isn't up there.  My GUI needs a lot of work - one of my big issues deals with making GUI clicks/keypresses separate from the rest of the world (mouse picking for movement), but I have that low on priorities right now.  :slight_smile:

One thing that had brought me to the idea using a thread is that I want stop sending messages to server (spamming) when there is no movement from a player at all.

So I am watching for events in the keyboard handler (inside GL Thread) and kick off sending only when player moves and as soon as the player stops i cancel the thread.

This works so far nice now.

PS: Hopefully we get one day better GUI support. :slight_smile:

This FengGUI drives me crazy with all the restrictions and caveats

Offtopic:  GUI can be a pain - I guess the best advice there is to make it something you can easily swap out for another GUI system, if need be.  I plan on trying FengGUI soon if I end up unable to figure out my input issues!

Parmenides said:

This FengGUI drives me crazy with all the restrictions and caveats

Its part of the Rendering Thread - so treat it as not thread safe just like jme