#include <iostream>
#include <thread>
#include <mutex>
static const int num_threads = 10 ;
std:: mutex mtx;
//This function will be called from a thread
void call_from_thread( int tid) {
std:: lock_guard < std:: mutex > lock( mtx) ;
std:: cout << "Launched by thread " << tid << std:: endl ;
}
int main( ) {
std:: thread t[ num_threads] ;
//Launch a group of threads
for ( int i = 0 ; i < num_threads; ++ i) {
t[ i] = std:: thread ( call_from_thread, i) ;
}
std:: cout << "Launched from the main\n " ;
//Join the threads with the main thread
for ( int i = 0 ; i < num_threads; ++ i) {
t[ i] .join ( ) ;
}
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dGhyZWFkPgojaW5jbHVkZSA8bXV0ZXg+CgpzdGF0aWMgY29uc3QgaW50IG51bV90aHJlYWRzID0gMTA7CgpzdGQ6Om11dGV4IG10eDsgCgovL1RoaXMgZnVuY3Rpb24gd2lsbCBiZSBjYWxsZWQgZnJvbSBhIHRocmVhZAoKdm9pZCBjYWxsX2Zyb21fdGhyZWFkKGludCB0aWQpIHsKCXN0ZDo6bG9ja19ndWFyZDxzdGQ6Om11dGV4PiBsb2NrKG10eCk7CiAgICBzdGQ6OmNvdXQgPDwgIkxhdW5jaGVkIGJ5IHRocmVhZCAiIDw8IHRpZCA8PCBzdGQ6OmVuZGw7Cn0KCmludCBtYWluKCkgewogICAgc3RkOjp0aHJlYWQgdFtudW1fdGhyZWFkc107CgogICAgLy9MYXVuY2ggYSBncm91cCBvZiB0aHJlYWRzCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG51bV90aHJlYWRzOyArK2kpIHsKICAgICAgICB0W2ldID0gc3RkOjp0aHJlYWQoY2FsbF9mcm9tX3RocmVhZCwgaSk7CiAgICB9CgogICAgc3RkOjpjb3V0IDw8ICJMYXVuY2hlZCBmcm9tIHRoZSBtYWluXG4iOwoKICAgIC8vSm9pbiB0aGUgdGhyZWFkcyB3aXRoIHRoZSBtYWluIHRocmVhZAogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBudW1fdGhyZWFkczsgKytpKSB7CiAgICAgICAgdFtpXS5qb2luKCk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=