Using OpenGL with Akka Actors: guaranteeing a sing

2019-05-08 18:41发布

问题:

I have a Scala/Java OpenGL application in which I use the Akka framework. At the moment, my OpenGL thread is independent from the actor system so I can guarantee calls to OpenGL functions always come from a single thread. This is important, otherwise OpenGL will complain.

Until now, I had to send messages from the OpenGL thread to actors in the system and this works pretty well. I am now faced with the need to send messages the other way around, but of course I cannot simply put the OpenGL in an actor as this will break the requirement that it runs from a single thread.

An alternative would be to use a queue and a lock manually for the communication between actors and the OpenGL thread, but I'd like to know if there's a way to place OpenGL calls in a special Actor that would be given a guarantee by Akka to run in a single thread.

Regards

回答1:

You can use a single thread per actor using a PinnedDispatcher. According to the doc:

This dispatcher dedicates a unique thread for each actor using it; i.e. each actor will have its own thread pool with only one thread in the pool.