From: jose guzman Date: Thu, 15 Oct 2015 09:51:36 +0000 (+0200) Subject: merge async example X-Git-Tag: v0.1~331^2 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=2eaf38cfdcbd2cfb0cc323dad6ded6bbeb436edf;p=cpPlugins.git merge async example --- 2eaf38cfdcbd2cfb0cc323dad6ded6bbeb436edf diff --cc appli/examples/CMakeLists.txt index 5174333,ee0d4fe..b762088 --- a/appli/examples/CMakeLists.txt +++ b/appli/examples/CMakeLists.txt @@@ -55,6 -37,6 +55,7 @@@ SET NOPLUGINS_EXAMPLES_PROGRAMS example_BaseInteractorStyle example_ContourWidget ++ example_Test_async ## example_Test_DoubleClick ## example_ExtractDICOMSeries ## example_ImageGaussianModelEstimator diff --cc appli/examples/example_Test_async.cxx index 0000000,efc9b94..e3cfd99 mode 000000,100644..100644 --- a/appli/examples/example_Test_async.cxx +++ b/appli/examples/example_Test_async.cxx @@@ -1,0 -1,79 +1,76 @@@ + #include + #include + #ifdef WIN32 + #include + #else + #endif + ++#ifdef WIN32 ++#define mysleep Sleep(2000); ++#else ++#define mysleep usleep(2000); ++#endif ++ + class Test + { + private: + std::vector state; + std::vector::iterator it; + std::vector isAvailable; + public: + Test() + { + this->it = this->state.begin(); + } + + void called_from_async() + { -#ifdef WIN32 - Sleep(2000); -#else - usleep(2000 * 1000); -#endif ++ + std::cout << "called from async" << std::endl; + this->state.insert(this->it ,10); + } + + std::string returned_from_async() { -#ifdef WIN32 - Sleep(2000); -#else - usleep(2000 * 1000); -#endif - //std::cout << "Async call" << std::endl; ++ mysleep; + return "Async call"; + } + + template + void print_results(ASYNC &f) + { + std::string result = f.get(); + std::cout << result.c_str() << std::endl; + } + + int getState(int pos) + { + return this->state.at(pos); + } + + int getStateSize() + { + return this->state.size(); + } + }; + + int main() { + Test test; + std::future result(std::async(&Test::called_from_async, &test)); + + auto f1 = std::async(std::launch::async, &Test::returned_from_async, &test); + + std::cout << "Message from main." << std::endl; + do + { + std::cout << test.getStateSize(); + } while (test.getStateSize()<1); + + #ifdef _DEBUG + auto i = 0; + std::cout << "wait for close"; + std::cin >> i; + #endif + + return 0; + + }