Take a look at the following sample,
#include <iostream>
#include <thread>
class GPS
{
public:
GPS() { this->init(); }
~GPS() {m_thread.join();}
private:
std::thread m_thread;
void init() { m_thread = std::thread(&GPS::update,this); }
void update() { /*get data from gps sensor.*/ };
};
GPS myGPS;
int main()
{
return 0;
}
What is/are the consequences of creating an object globally that has its own thread? Is this safe approach? if No, what are the alternatives with the assumption that the object must be global and has an independent thread? Thank you.