Skip to content

Instantly share code, notes, and snippets.

@hmenn
Created February 25, 2019 13:51
Show Gist options
  • Save hmenn/ced563a7ca7fb0eb7e398061590d1d07 to your computer and use it in GitHub Desktop.
Save hmenn/ced563a7ca7fb0eb7e398061590d1d07 to your computer and use it in GitHub Desktop.
basic thread example from modern cpp concurrency in depth
#include <iostream>
#include <thread>
enum COMMAND{CLEAN, FULL_SPEED_AHEAD, EXIT};
int main() {
int cmd;
bool done=false;
while(!done){
std::cout<<"Enter command:";
std::cin>>cmd;
if(cmd==COMMAND::CLEAN){
std::thread th([](){
std::cout<<"Cleaning..."<<std::endl;
});
th.detach();
}else if( cmd ==COMMAND::FULL_SPEED_AHEAD){
std::thread th([](){
std::cout<<"Full speed ahead..."<<std::endl;
});
th.join();
std::cout<<"Full speed done"<<std::endl;
}else if(cmd==COMMAND::EXIT){
done=true;
}else{
std::cerr<<"Invalid command."<<std::endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment