implementation of asynchronous thread

50 views
Skip to first unread message

kushal bhattacharya

unread,
Dec 3, 2016, 1:02:48 AM12/3/16
to ISO C++ Standard - Discussion
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

ltpmouse

unread,
Dec 3, 2016, 1:10:44 AM12/3/16
to ISO C++ Standard - Discussion


在 2016年12月3日星期六 UTC+8下午2:02:48,kushal bhattacharya写道:
 
std::thread([]{ do_some_work(); do_some_more(); }).detach();

kushal bhattacharya

unread,
Dec 3, 2016, 1:14:14 AM12/3/16
to ISO C++ Standard - Discussion
yes i have tried this. it isnt a foolproof mechanism.The thing i want is  if i  create a thread then that thread and the main thread and other threads if present they will run run parallellly without having to wait for the other to complete.With detach it gives away all the thread resources and it doesn't give the desired output

kushal bhattacharya

unread,
Dec 3, 2016, 5:06:53 AM12/3/16
to ISO C++ Standard - Discussion
i have tried using async with future along with asynch_launch but i get the same behaviour as thread.join()

Anthony Williams

unread,
Dec 3, 2016, 5:10:43 AM12/3/16
to std-dis...@isocpp.org

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

kushal bhattacharya

unread,
Dec 3, 2016, 6:34:48 AM12/3/16
to ISO C++ Standard - Discussion
heres my code which uses async along with future. in every instance of paho_client which is used inside each thread,its member function contains infinite loop.
I am posting the 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;
}
#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;
}

kushal bhattacharya

unread,
Dec 4, 2016, 2:17:50 AM12/4/16
to ISO C++ Standard - Discussion
to be more specific -:
The thing is i want to use c++ library which run differents threads simultaneously without having other threads to wait until the preceding thread is complete and their functionality within each thread is run simultaneuslly,I am talking about the code which is to be run in the thread;the sample code is shown below.


       while(condition is true<it is infinite loop >){
       running sleep here with random time
         sleep(random time(sec))
         rest of the code is here
       }


this infinite while loop is run in each thread. i want to run this while loop in each thread to be run sumultaneously without being stuck at the first thread to be completed. In other words all the infinite while loop(in each thread context ) is to be run simultaneously.How do i acheive that.If you can please share some sample code actually i have used future with async but i get the same behaviour as normal <thread> using join().

Anthony Williams

unread,
Dec 5, 2016, 4:05:08 AM12/5/16
to std-dis...@isocpp.org
On 03/12/16 11:34, kushal bhattacharya wrote:
> heres my code which uses async along with future. in every instance of
> paho_client which is used inside each thread,its member function
> contains infinite loop.

Your problem is here:

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

You create the async task, which will now run in the background. You
then immediately wait for the result by calling f.get(). This will block
the main thread until the background thread has completed, thus
preventing you from spawning new tasks.

Instead, you should store the futures in a vector or other container,
and then wait for the results *after* you have spawned all the tasks.

Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Reply all
Reply to author
Forward
0 new messages