Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8605147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:45:13+00:00 2026-06-12T02:45:13+00:00

I’m running into a mild conundrum concerning thread safety for my game loop. What

  • 0

I’m running into a mild conundrum concerning thread safety for my game loop. What I have below is 3 threads (including the main) that are meant to work together. One for event managing (main thread), one for logic, and one for the rendering. All 3 of these threads exist within their own class, as you can see below. In basic testing the structure works without problems. This system uses SFML and renders with OpenGL.

int main(){
    Gamestate gs;
    EventManager em(&gs);
    LogicManager lm(&gs);
    Renderer renderer(&gs);

    lm.start();
    renderer.start();
    em.eventLoop();

    return 0;
}

However, as you may have noticed I have a “Gamestate” class that is meant to act as a container of all the resources that need to be shared between the threads (mostly with LogicManager as a writer and Renderer as a reader. EventManager is mostly just for window events). My questions are: (1 and 2 being the most important)

1) Is this a good way of going about things? Meaning is having a “global” Gamestate class a good idea to use? Is there a better way of going about it?

2) My intention was to have Gamestate have mutexes in the getters/setters, except that doesn’t work for reading because I can’t return the object while it’s still locked, which means I’d have to put synchronization outside of the getters/setters and make the mutexes public. It also means I’d have a bloody ton of mutexes for all the different resources. What is the most elegant way of going about this problem?

3) I have all of the threads accessing “bool run” to check if to continue their loops

while(gs->run){
....
}

run gets set to false if I receive a quit message in the EventManager. Do I need to synchronize that variable at all? Would I set it to volatile?

4) Does constantly dereferencing pointers and such have an impact on performance? eg gs->objects->entitylist.at(2)->move(); Do all those ‘->’ and ‘.’ cause any major slowdown?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:45:15+00:00Added an answer on June 12, 2026 at 2:45 am

    Global state

    1) Is this a good way of going about things? Meaning is having a “global” Gamestate class a good idea to use? Is there a better way of going about it?

    For a game, as opposed to some reusable piece of code, I’d say a global state is good enough. You might even avoid passing gamestate pointers around, and really make it a global variable instead.

    Synchronization

    2) My intention was to have Gamestate have mutexes in the getters/setters, except that doesn’t work for reading because I can’t return the object while it’s still locked, which means I’d have to put synchronization outside of the getters/setters and make the mutexes public. It also means I’d have a bloody ton of mutexes for all the different resources. What is the most elegant way of going about this problem?

    I’d try to think of this in terms of transactions. Wrapping every single state change into its own mutex locking code will not only impact performance, but might lead to actually incorrect behaviour if the code gets one state element, performs some computation on it and sets the value later on, while some other code modified the same element in between. So I’d try to structure LogicManager and Renderer in such ways that all the interaction with the Gamestate occurs bundled in a few places. For the duration of that interaction, the thread should hold a mutex on the state.

    If you want to enforce the use of mutexes, then you can create some construct where you have at least two classes. Let’s call them GameStateData and GameStateAccess. GameStateData would contain all the state, but without providing public access to it. GameStateAccess would be a friend of GameStateData and provide access to its private data. The constructor of GameStateAccess would take a reference or pointer to the GameStateData and would lock the mutex for that data. The destructor would free the mutex. That way, your code to manipulate the state would simply be written as a block where a GameStateAccess object is in scope.

    There is still a loophole, though: In cases where objects returned from this GameStateAccess class are pointers or references to mutable objects, then this setup won’t keep your code from carrying such a pointer out of the scope protected by the mutex. To prevent this, either take care about how you write things, or use some custom pointer-like template class which can be cleared once the GameStateAccess goes out of scope, or make sure you only pass things by value not reference.

    Example

    Using C++11, the above idea for lock management could be implemented as follows:

    class GameStateData {
    private:
      std::mutex _mtx;
      int _val;
      friend class GameStateAccess;
    };
    GameStateData global_state;
    
    class GameStateAccess {
    private:
      GameStateData& _data;
      std::lock_guard<std::mutex> _lock;
    public:
      GameStateAccess(GameStateData& data)
        : _data(data), _lock(data._mtx) {}
      int getValue() const { return _data._val; }
      void setValue(int val) { _data._val = val; }
    };
    
    void LogicManager::performStateUpdate {
      int valueIncrement = computeValueIncrement(); // No lock for this computation
      { GameStateAccess gs(global_state); // Lock will be held during this scope
        int oldValue = gs.getValue();
        int newValue = oldValue + valueIncrement;
        gs.setValue(newValue); // still in the same transaction
      } // free lock on global state
      cleanup(); // No lock held here either
    }
    

    Loop termination indicator

    3) I have all of the threads accessing “bool run” to check if to continue their loops

    while(gs->run){
    ....
    }
    

    run gets set to false if I receive a quit message in the EventManager. Do I need to synchronize that variable at all? Would I set it to volatile?

    For this application, a volatile but otherwise unsynchronized variable should be fine. You have to declare it volatile in order to prevent the compiler from generating code which caches that value, thus hiding a modification by another thread.

    As an alternative, you might want to use a std::atomic variable for this.

    Pointer indirection overhead

    4) Does constantly dereferencing pointers and such have an impact on performance? eg gs->objects->entitylist.at(2)->move(); Do all those -> and . cause any major slowdown?

    It depends on the alternatives. In many cases, the compiler will be able to keep the value of e.g. gs->objects->entitylist.at(2) in the above code, if it is used repeatedly, and won’t have to compute it over and over again. In general I would consider the performance penalty due to all this pointer indirection to be of minor concern, but that is hard to tell for sure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.