std::thread([]{ do_some_work(); do_some_more(); }).detach();
On 3 Dec 2016 10:06, "kushal bhattacharya" <bhattachar...@gmail.com> wrote:
>
> i have tried using async with future along with asynch_launch but i get the same behaviour as thread.join
Can you show some code with what you would like to happen, then we can show you how to achieve it.
Anthony
#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;
}