hi,
I am unable to find a correct usage of asynchronous thread in c++11.The thing i want to do is i want to spwan threads and each thread will function simultaneously without waiting for each other like thread.join(),which makes other thread to wait until the current thread is done with.So,is there any library in c++ which makes threads to run parallely doing their work simultaneously without having to wait for the other to complete.
Thanks,
Kushal
could please share sample code as to how i can acheive that.What i want is ,i want to launch the jobs as threads and then later wait for the result.i dont want to wait for threads to complete. if you recommend detach() it doesn't work for my scenario
i have used async along with future in the code but still it behaves like thread with join.Actually am using object within each thread.the member function has infinite loop so the execution is stuck to the 1st thread only,It only prints the messages of the 1st thread ,I am sharing my code below->
int main(int argc, char** argv) {
signal(SIGINT, Onfinish);
if(argc<3){
cout<<"the format of starting commandline argument is ./paho_client_emulate <create_publisher><count of publisher client to spawn>"<<endl;
exit(1);
}
while(finish_thread!=0){
// paho_client::get_library_handle();
int topic_index;
if(strcmp(argv[1],"publisher")==0){
for(topic_index=0;topic_index<atoi(argv[2]);topic_index++){
// thread pub_th;
// cout<<"the number of publisher to create"<<atoi(argv[2])<<endl;
// pub_th = thread([ = ]() {
if(topic_index==(atoi(argv[2])-1)){
cout<<"broken out of loop"<<endl;
break;
}
future<int> f = async(std::launch::async, accumulate_block_worker_ret,topic_index);
// });
// pub_th.join();
cout<<"the returned value from future is"<<f.get()<<endl;
}