I have two threads, one of them may throw an exception, I want to make the thread restart if it throws an exception. Here is my code:
#include <iostream>
#include <future>
#include <chrono>
void func1()
{
int i = 0;
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout<<"func1"<<std::endl;
if (i++ == 3)
{
throw 2;
}
}
}
void func2()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout<<"func2"<<std::endl;
}
}
int main()
{
std::future<void> res(std::async(std::launch::async, func1));
std::future<void> res2(std::async(std::launch::async, func2));
try{
res.get();
res2.get();
}catch(int e){}
return 0;
}
When res.get()
throws the exception, res2
is stilling running, in this case, how could I make the res
to restart?
Another thing is, if I first res2.get()
then res.get()
, the exception of res
will be thrown but will be never caught, in this case, how could I handle the res
? It doesn't seem that I can touch and control it anymore unless the res2.get()
returns.
Aucun commentaire:
Enregistrer un commentaire