Currently I start my threads and wait for it to finish:
void ClassA::StartTest() // btn click from GUI
{
ClassB classB;
std::vector<std::thread> threads;
for(int counter=0; counter<4; counter++)
{
threads.at(counter) = std::thread(&ClassB::ExecuteTest, classB);
// if I join the threads here -> no parallelism
}
// wait for threads to finish
for(auto it=threads.begin(); it!=threads.end(); it++)
it->join();
}
ClassB
#include <mutex>
ClassB
{
public:
void ExecuteTest(); // thread function
private:
std::mutex m_mutex;
bool ExecuteOtherWork(std::string &value);
};
Relevant method ExecuteTest()
void ClassB::ExecuteTest()
{
std::string tmp;
std::lock_guard<std::mutex> lock(m_mutex); // lock mutex
std::stringstream stream(pathToFile);
while(getline(stream, tmp, ',')) // read some comma sep stuff
{
if(!ExecuteOtherWork(tmp)) break;
}
}
All ok, but I want to have a thread timeout: lets say after 40sec the threads have to quit there work and return to main thread. How can I do that?
Thx!