LWJGL, Clojure, Single Thread for OpenGL Commands LWJGL, Clojure, Single Thread for OpenGL Commands multithreading multithreading

LWJGL, Clojure, Single Thread for OpenGL Commands


The usual way I would design this would be just to have a single thread responsible for all rendering. By doing this you avoid the need for other threads to "issue OpenGL calls".

Of course, you still need a way to get information to the rendering thread, but that can be handled with Clojure's standard concurrency techniques. For example, if your world state is immutable you could just use an atom for state updates, and have the rendering thread just perform rendering using the latest world state stored in the atom:

  (def world-state (atom (initial-world-state)))  ;; in rendering thread  .....  (loop []    (render-world @world-state) ; all OpenGL calls happen in this function!!    (other-stuff)    (if (still-running) (recur)))  ;; in other thread responsible for updating world  (loop []    (swap! world-state update-world-function)    (other-stuff)    (sleep-if-needed)    (if (still-running) (recur)))