1

Is it possible with any existing c++ library to implement a callback that returns when a query is completed?

I've found this, but I'm not sure if that's what I want.

I'd like to wait the boost::thread writing to the database until the write is completed.

If this is possible, please link the library and an example.

4
  • Just join the thread don't work for you? Commented Sep 12, 2013 at 23:02
  • @MatheusOl thank you for looking! i don't want to block. i simply want the thread that's performing the database query to wait/sleep until the query is finished. i don't want that thread to block cores from being used by other threads while waiting for the query's response. Commented Sep 12, 2013 at 23:05
  • how are you sending the query to the database? Generally you should sent it synchronously, so you don't need to worry about that, after sending the query (e.g. PQexec) the thread will already "wait" until the query is finished. Commented Sep 12, 2013 at 23:25
  • Ok. I'll send an answer to clarify that. Commented Sep 12, 2013 at 23:27

2 Answers 2

2

I was looking for something like this too, ended up doing an async libpq wrapper: http://github.com/metherealone/postgrespp - It uses Boost.ASIO though, not threads. I hope this helps.

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

Comments

1

From the libpq docs about PQexec function:

Submits a command to the server and waits for the result.

Similar to that, the PQexecParams and PQexecPrepared also wait for the query to be executed. So, using those functions you don't need to worry about waiting, the API will do that for you.

If you need to asynchronously send query to the database, you can use the asynchronous functions.

5 Comments

will the boost thread continue to use the core while it waits, or will that core be freed so that other threads can use it?
@Gracchus: I don't know exactly what you meant by "core". But the thread will (kind of) sleep until the query returns, but, of course, it does not use CPU time while it is sleeping.
so while the thread that's trying to write to the database is waiting for the query result, other boost threads will automatically take its place and use the CPU?
Yes. There is actually some work on that thread, but if the query is too slow there will be almost no work on the client side. Notice that if your PG server will consume CPU to process this query, so if it is local, there will be concurrency to resources with your boost threads.
Just to clarify, it is not the boost that "chooses" what thread will run on each CPU core, it is a job done by the OS and boost has no control over it. So, you can run 100 threads on a 4 core machine, that the OS's scheduler will take care of controlling them.

Your Answer

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