I have an application where I need to query a database to get/put information. I can’t do it synchronously as it would block my entire process until the function returns.
Basically I have a few functions that run one or more queries at certain points.
fun
stuff1
stuff2
stuff3
query1
stuff4
query2
stuff5
-
I could start the functions in separate threads, but then I would have to lock everything to prevent races (I think locking could be slow ?)
-
I could start the queries asynchronously and monitor them but then I would have to split my functions and use callbacks that would run when the qouery is over
I am interested in a general solution, but my platform is POSIX and the database is (unfortunately) mysql.
What would you do ? How would you handle this ?
Thank you for your time.
There are patterns that have been known and used for quite sometime and I believe they have not changed.
EventWaitHandlethat can wait for multiple threads and notify when all finished.Task<T>chaining which makes reading and writing the code much simpler. jquery now offerspromisewhich is the same thing.