0

I am using MySQL to store data for a game I am making and need to access the database from outside the main thread so to not interupt it from a long query and return the data to the main thread to be processed on it's next loop. I am aware that there are multiple ways of doing this, queues, maps, callbacks etc.

What would be the "accepted" way of doing this if I was asked to do this professionally?

I know this is a broad, slightly opinion based question but this is the only place I know of that I might get an answer from, if anyone knows a site that is more suited to questions like this please let me know.

Thanks.

2
  • How frequent do you need to to query data from DB? And what does the main thread do if the data if not returned yet? Commented Jan 21, 2014 at 3:26
  • The main thread would continue looping and carrying out it's tasks, when the data is returned it would trigger an action such a displaying text or update/set variables. Commented Jan 21, 2014 at 4:10

1 Answer 1

1

I wrote and designed an asynchronous database project which is in use at my workplace. I used the Observer pattern (callbacks) and it worked very well for us. This is actually the approach we take with all asynchronous code we write. Register for an callback, make the async call, respond appropriately in the callback.

I work on games as a hobby. If I were doing this in a game I'd probably use the same approach. Just remember the callback will be on a different thread. Be sure to execute updates on your main thread.

In the end its going to depend on what make's sense for the rest of your code though. Do what you think will be cleanest. Notice I didn't say easiest. The simpler your logic is to follow, the simpler it will be to maintain and improve.

Sign up to request clarification or add additional context in comments.

5 Comments

What is the best way to execute updates on the main thread from a callback on a seperate thread?
Through a thread-safe data structure. Android has the Handler class. Swing has EventQueue. They're both doing the same thing though; allowing you to execute code on another thread. Handler and EventQueue are the threadsafe datastructure @user2248702
Would you be able to give an example?
I can if you want, despite there being many tutorials online... which do you want an example for? Android or Swing?
A link to a good tutorial would be fine thanks, I am not using android or swing (making a server for web chat) so it would be great if there is a more general purpose tutorial. Thank you for answering my extremely noobish questions, it has been amazingly helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.