Created
February 25, 2019 13:51
-
-
Save hmenn/ced563a7ca7fb0eb7e398061590d1d07 to your computer and use it in GitHub Desktop.
basic thread example from modern cpp concurrency in depth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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