If I need to improve an existing code by using a thread pool, how can I get data from a SQL DB:
Code:
bool CMyClass::GetSQLSelect(std::string sql, void* data)
{
....
}
and suppose I have another code, higher level:
Code:
void CMyClass::DoSomething()
{
void* data = nullptr;
if (!GetSQLSelect("SELECT * FROM my_table", data))
return;
std::string result = static_cast<std::string>(data);
// do something with result
}
Now, all examples from internet give me some examples of how to add some methods to a thread pool, but those methods are not returning data to the main thread. What if I need data from those posted to thread methods ?
I have found and example right here: https://stackoverflow.com/questions/...pooling-in-c11.
I have seen something very simple using boost:
Code:
void my_task() { ... }
int main()
{
int threadNumbers = thread::hardware_concurrency();
boost::asio::thread_pool pool(threadNumbers);
// Submit a function to the pool.
boost::asio::post(pool, my_task);
// Submit a lambda object to the pool.
boost::asio::post(pool, []() {
...
});
}
but how can I use it in my case, when I expect data from posted method ? Something like that:
Code:
std::string sql = "SELECT * from my_table";
void* data = nullptr;
boost::asio::post(GetSQLSelect, sql, data);
but when I retrieve data then ?
Every hint, even small one, I will appreciate it most !