CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jan 2009
    Posts
    399

    Using a thread pool in real situations

    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 !
    Last edited by mesajflaviu; April 19th, 2021 at 03:44 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured