I'm trying to use Tyler Hardin's thread pool class. The library can be found here: https://github.com/Tyler-Hardin/thread_pool
My code is:
#include "thread_pool.hpp"
#include <windows.h>
#include <iostream>
#include <list>
#include <string>
#include <sstream>
using namespace std;
const int num_threads = 8;
int getRandom(int min, int max)
{
return min + rand() % (max - min);
}
std::string to_string(int val)
{
std::ostringstream ss;
ss << val;
std::string str = ss.str();
return str;
}
string getResult(string param)
{
int time = getRandom(0, 500);
Sleep(time);
return ("Time spend here: " + to_string(time));
}
int main()
{
srand(time(NULL));
thread_pool pool(num_threads);
list<future<string>> results;
for(int i=100; i<=100000; i++)
{
std::future<string> buff = pool.async( function<string(string)>(getResult), "MyString" );
results.push_back( buff );
}
for(auto i=results.begin(); i != results.end(); i++)
{
i->get();
cout << endl;
}
return 0;
}
But something seems to be wrong as I am facing with the following errors:
error: no matching function for call to 'thread_pool::async(std::function<std::basic_string<char>(std::basic_string<char>)>, const char [9])
error: use of deleted function 'std::future<_Res>::future(const std::future<_Res>&) [with _Res = std::basic_string<char>]'|
What am I doing wrong in this call:
std::future<string> buff = pool.async( function<string(string)>(getResult), "MyString" );
The program should print the sleep time withing every thread right after every thread finish their job.
getRandom, why not simply have thesrandcall in themainfunction before you do anything? Because the way it is now it's not thread-safe.std::to_string().