This question already has an answer here:
- std::async doesn't work asynchronously 2 answers
Why async call behavelike sync call ?.In the below example mTest function called with std::sync with launch policy async & with normal thread one by one seperately.Why Both API behaviours are different in output?
#include <iostream>
#include <Test.h>
#include <future>
Test::Test()
{
}
Test::~Test()
{
}
void Test::doTest(){
cout << std::this_thread::get_id() << " doTest" << endl;
// std::async(std::launch::async, &Test::mTest, this);
mThread = std::thread(&Test::mTest,this);
cout << std::this_thread::get_id() << " after async call" << endl;
if (mThread.joinable()) mThread.join();
}
bool Test::mTest()
{
cout <<std::this_thread::get_id()<< " Asyn mTest called"<<endl;
std::this_thread::sleep_for(chrono::milliseconds(10000));
return true;
}
int main(int argc, char **argv) {
Test test;
test.doTest();
}
Output:
using async call
rajesh@rajesh:~/cpp_test$ ./test
140079678486336 doTest
140079660783360 Asyn mTest called
140079678486336 after async call
using Thread call
rajesh@rajesh:~/cpp_test$ ./test
139787779311424 doTest
139787779311424 after async call
139787761608448 Asyn mTest called
Aucun commentaire:
Enregistrer un commentaire