2

I am trying to call GLFW.glfwPollEvents() in an asynchronous task that runs every tick (1/30th of a second in this case). This ticking timer effectively controls when every action in the app takes place.

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
    @Override
    public void run(){
        //... more code ...

        GLFW.glfwPollEvents();

        //... more code ...
    }
}, 33, 33);

But, this does not work because

This function may only be called from the main thread.

(from the documentation)

How can I call this on the main thread? When it isn't run on the main thread, the application crashes. I'm looking for something like

GLFW.dispatchMainThread(new GLFWRunnable(){
    public void run(){
        //...
    }
});

This is possible to do in swing and awt by using

EventQueue.invokeLater(new Runnable(){
    public void run(){
        //...
    }
});

But the same code doesn't work for GLFW.

How can I run a task on GLFW's main thread using LWJGL without using a while(true) loop on the main thread?

Community
  • 1
  • 1
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • Could you add more detail about what specifically your main thread and your separate thread are doing, how the separate thread was started, should end, etc., and just the general relationship between the main and separate threads? – Sam Estep Jul 08 '15 at 00:37
  • Possible duplicate of [this](http://stackoverflow.com/questions/16268693/running-code-on-the-main-thread-from-a-secondary-thread); does the accepted answer there help? – Sam Estep Jul 08 '15 at 00:44
  • @RedRoboHood No. This question is about getting the main thread on `GLFW`, not in general, as `GLFW` may have a built-in implementation. Like I say in the question, it's possible to do with `Swing`. – Jojodmo Jul 08 '15 at 00:47
  • So the code that initializes and schedules `timer` is *not* being run on the GLFW main thread? – Sam Estep Jul 08 '15 at 00:52
  • Are there any GLFW callbacks you can set that you could trigger from the separate thread? – Sam Estep Jul 08 '15 at 01:00
  • @RedRoboHood Thats what this question is about - finding *something* that can make it run on the main thread – Jojodmo Jul 08 '15 at 01:00
  • I don't see any way to do what you're asking. Could you explain in your question why exactly you can't put a loop in the main thread? – Sam Estep Jul 08 '15 at 01:08
  • No, it won't. As the answer in the question I linked to said, you could have the main thread do other work in the loop as well and just check at intervals. Or you could use a sleep/notify system. There are many options that result in very little wasted memory and CPU cycles that you can make use of with a loop in the main thread. – Sam Estep Jul 08 '15 at 01:15
  • @RedRoboHood True, but are there any *built-in* ways to do it with GLFW? They would be much better than anything loop that I would make. – Jojodmo Jul 08 '15 at 01:18
  • As I said, none exist that I know of. I'm pretty sure GLFW is designed to be used with a main loop. – Sam Estep Jul 08 '15 at 01:19

1 Answers1

2

Since GLFW's main thread must be the same as the application's main thread (a limitation in GLFW; that's where it puts the OS message queue that is handled differently by swing/awt it seems - see my answer here) I would do things the other way around.

My main thread would have the forever loop (probably using glfwWaitEvents so as not to eat CPU time needlessly). I would then post any events that were app-specific to another thread that does the processing. Your message processing is now decoupled from your message receiving.

This way I don't have to wait for 1/30th of a second to get OS messages (it always frustrates me when an app lags opening a menu, registering a click, or arrowing through a list).

Your update thread can even sleep for 1/30th of a second (or less, sleeping again if not enough time has passed or not if a second high-priority queue needs work) and only wake up to check for queued events as an extremely simple scheduling method.

The answer I linked to above has a link to an LWJGL Multithreaded Demo that does something very similar. Their renderLoop would be your updateLoop and would draw from a queue and do processing before going to sleep again instead of updating and presenting graphics.

Community
  • 1
  • 1
A Sammich
  • 366
  • 1
  • 5
  • 13