Created
May 27, 2020 09:00
-
-
Save pthom/b18c167d47ed5de582604756e503aca0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <atomic> | |
#include <future> | |
#include <iostream> | |
class AsyncRaiiExample | |
{ | |
public: | |
AsyncRaiiExample() : mStopRequired(false) | |
{ | |
mThreadProcResult = std::async( std::launch::async, | |
[this](){ | |
this->ThreadProc(); | |
}); | |
} | |
~AsyncRaiiExample() | |
{ | |
mStopRequired = true; | |
mThreadProcResult.get(); | |
} | |
void ThreadProc() | |
{ | |
while ( ! mStopRequired) | |
{ | |
std::cout << "Hello" <<std::endl; | |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); | |
} | |
std::cout << "Stopping ...\n"; | |
} | |
void Stop() | |
{ | |
mStopRequired = true; | |
} | |
private: | |
std::atomic<bool> mStopRequired; | |
std::future<void> mThreadProcResult; | |
}; | |
int main() | |
{ | |
{ | |
AsyncRaiiExample t; | |
std::this_thread::sleep_for(std::chrono::seconds(1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code minimal a compiler avec
Et tu lances ensuite