# Main thread task context API (LWJGL)

**URL:** <https://discourse.processing.org/t/main-thread-task-context-api-lwjgl/17226>\
**Category:** Development\
**Created:** [January 21, 2020, 12:50pm UTC](https://discourse.processing.org/t/main-thread-task-context-api-lwjgl/17226 "2020-01-21T12:50:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [January 21, 2020, 12:50pm UTC](https://discourse.processing.org/t/main-thread-task-context-api-lwjgl/17226/1 "2020-01-21T12:50:08Z")

</div>

I’ve no idea why this issue was closed - [https://github.com/processing/processing4/issues/65](https://github.com/processing/processing4/issues/65) It makes trying to contribute to any development and discussion on Processing development exceedingly frustrating!

I’ve been doing some work on the LWJGL renderer recently. If Processing is ever to fully support an LWJGL renderer, there needs to be a way to run certain tasks on the main (first) thread. What would be thoughts on an API something similar to this in PApplet?

```java
public static MainThreadContext mainThread() {...}
public static void setMainThreadContext(MainThreadContext context) {...}

interface MainThreadContext {
  void invokeLater(Runnable task);
  boolean isMainThread();
}

```

Code at the end of `main()` would have to loop over a task queue. Setting context option is in there for any other language / tooling support, and multiple windows, that need to start sketches in another fashion.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 22, 2020, 3:10pm UTC](https://discourse.processing.org/t/main-thread-task-context-api-lwjgl/17226/2 "2020-01-22T15:10:57Z")

</div>

I don’t know enough yet to say anything about it. Could you maybe compare how it is now to how it would be? Do you have more than one possible implementations in mind?

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [January 22, 2020, 5:02pm UTC](https://discourse.processing.org/t/main-thread-task-context-api-lwjgl/17226/3 "2020-01-22T17:02:35Z")

</div>

LWJGL has a hard requirement (well, certain OS do) that all UI creation and event handling happen in the first thread of the application - let’s call that the initial thread that runs `main()`, although it’s a little more complicated than that! 😄 JOGL hides this fact from you, which is also why it doesn’t perform as well. Actual OpenGL rendering can be in another thread though, and should be in Processing for a variety of reasons.

The current LWJGL renderer that exists gets around this by running everything inside `startThread`, but this makes this renderer behave differently to all the others and will break certain things. What’s needed to make this renderer function correctly is to split off the UI tasks from rendering. To do that, an API similar to that suggested needs to exist. For a default implementation, the end of `main()` would just loop on a queue and run any tasks added to it. However, any embedding (eg. in PraxisLIVE) and potentially other modes would need to be able to set an alternative implementation.

I’m busy working on this, but would ideally do something that can be contributed back.

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [January 23, 2020, 7:21pm UTC](https://discourse.processing.org/t/main-thread-task-context-api-lwjgl/17226/4 "2020-01-23T19:21:43Z")

</div>

Spent a couple of hours working on a proof of concept for this -

> <https://github.com/praxis-live/processing-core/blob/master/src/main/java/processing/core/PApplet.java#L15986>

  

> <https://github.com/praxis-live/processing-lwjgl/blob/master/src/main/java/processing/lwjgl/PSurfaceLWJGL.java>

My favourite example sketch running well -

 ![Processing-LWJGL-Esfera](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/4a96ce7f010b1ff7a1ccfb627676913d46777bfd.png)
