SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
AsyncWorker Class Reference

AsyncWorker implements a async worker thread. More...

#include <AsyncWorker.h>

Public Member Functions

virtual ~AsyncWorker ()
 
void start ()
 
void stop ()
 
bool isReady ()
 if returns true, results are valid to be retrieved More...
 

Protected Member Functions

bool stopRequested ()
 
void setReady ()
 
virtual void run ()
 

Private Attributes

std::thread _thread
 
std::atomic_bool _stop {false}
 
std::atomic_bool _ready {false}
 

Detailed Description

AsyncWorker implements a async worker thread.

Definition at line 19 of file AsyncWorker.h.

Constructor & Destructor Documentation

◆ ~AsyncWorker()

AsyncWorker::~AsyncWorker ( )
virtual

Definition at line 15 of file AsyncWorker.cpp.

16 {
17  stop();
18 }

Member Function Documentation

◆ isReady()

bool AsyncWorker::isReady ( )

if returns true, results are valid to be retrieved

Definition at line 36 of file AsyncWorker.cpp.

37 {
38  return _ready;
39 }
std::atomic_bool _ready
Definition: AsyncWorker.h:36

◆ run()

void AsyncWorker::run ( )
protectedvirtual

Definition at line 52 of file AsyncWorker.cpp.

53 {
54  int n = 120;
55  int i = 0;
56  // do task
57  while (true)
58  {
59  using namespace std::chrono_literals;
60  std::this_thread::sleep_for(100ms);
61  Utils::log("AsyncWorker", "run til %d: %i", n, i);
62  if (stopRequested())
63  {
64  Utils::log("AsyncWorker", "stop requested");
65  break;
66  }
67  if (i == n)
68  break;
69 
70  i++;
71  }
72 
73  // task is ready
74  Utils::log("AsyncWorker", "run ready");
75  setReady();
76 }
void setReady()
Definition: AsyncWorker.cpp:47
bool stopRequested()
Definition: AsyncWorker.cpp:41
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103

◆ setReady()

void AsyncWorker::setReady ( )
protected

Definition at line 47 of file AsyncWorker.cpp.

48 {
49  _ready = true;
50 }

◆ start()

void AsyncWorker::start ( )

Definition at line 20 of file AsyncWorker.cpp.

21 {
22  _ready = false;
23  _thread = std::thread(&AsyncWorker::run, this);
24 }
std::thread _thread
Definition: AsyncWorker.h:34
virtual void run()
Definition: AsyncWorker.cpp:52

◆ stop()

void AsyncWorker::stop ( )

Definition at line 26 of file AsyncWorker.cpp.

27 {
28  _stop = true;
29  if (_thread.joinable())
30  _thread.join();
31  _stop = false;
32  _ready = false;
33 }
std::atomic_bool _stop
Definition: AsyncWorker.h:35

◆ stopRequested()

bool AsyncWorker::stopRequested ( )
protected

Definition at line 41 of file AsyncWorker.cpp.

42 {
43  return _stop;
44 }

Member Data Documentation

◆ _ready

std::atomic_bool AsyncWorker::_ready {false}
private

Definition at line 36 of file AsyncWorker.h.

◆ _stop

std::atomic_bool AsyncWorker::_stop {false}
private

Definition at line 35 of file AsyncWorker.h.

◆ _thread

std::thread AsyncWorker::_thread
private

Definition at line 34 of file AsyncWorker.h.


The documentation for this class was generated from the following files: