writing sample c++ programs related to threads with multiple features in c++. The below program throws a runtime error at t1.join() using a switch statement however it is working plainly. Trying to understand, why it's throwing an error specifically with the switch. Does t1.join() not able to find the thread which started?
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument
#include <iostream>
#include <thread>
enum THREAD_CALLBACK_TYPE {
FUNC_PTR = 1,
FUNC_OBJ,
LAMBDA
};
//1. function pointer
void funPtr()
{
std::cout<<"From func pointer"<<std::endl;
}
//2. function object
class FuncObj
{
public:
void operator()()
{
std::cout<<"From function object"<<std::endl;
}
};
//3. lambda
auto Lambda = [](){
std::cout<<"From Lambda"<<std::endl;
};
int main()
{
std::cout<< "choose one option \n 1. "
"Function Pointer\n 2. Function Object\n 3. Lambda\n"<<std::endl;
std::cout<<"option: "<<std::endl;
uint8_t option = 0;
std::cin>>option;
std::thread t1;
//t1 = std::thread(Lambda);
switch (option)
{
case FUNC_PTR:
{
t1 = std::thread(funPtr);
}
break;
case FUNC_OBJ:
{
t1 = std::thread(FuncObj());
}
break;
case LAMBDA:
{
t1 = std::thread(Lambda);
}
break;
default:
break;
}
t1.join();
}
searched on StackOverflow. no such related questions were posted.