implementation of asynchronous thread

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
I think that C++11 thread can do the work like you want.
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 think you are looking for <future>

http://www.cplusplus.com/reference/future/
Last edited on
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->


#include <signal.h>
#include <thread>
#include <algorithm>
#include <cstring>
#include <csignal>
#include <future>
#include "paho_client.h"
using namespace std;
vector<string> topic_container{"rpi2/temp","sense/apple","sense/bannana","sense/util","mqtt/temp","sense/temp","sense/pine","sense/fortis/udap"};
vector<paho_client> publisher;
vector<paho_client> subscriber;
int finish_thread=1;
void Onfinish(int signum){
finish_thread=0;
exit(EXIT_FAILURE);
}
int accumulate_block_worker_ret(int topic_index) {
//int topic_index=0;
paho_client client("publisher", "192.168.0.102", "9876",
topic_container[topic_index].c_str());
client.paho_connect_withpub();
publisher.push_back(client);
cout<<"the size of client container is"<<publisher.size()<<endl;
client.increment_count();
return client.get_count();
}


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;
}

vector<paho_client>::iterator it;
int publisher_traverse=0;
// for(it=publisher.begin();it<=publisher.end();publisher_traverse++){
// cout<<"came here"<<endl;
// publisher[publisher_traverse].increment_count();
//publisher[publisher_traverse].get_count();
//}

}
}
return 0;
}
Topic archived. No new replies allowed.