Answer the question
In order to leave comments, you need to log in
Why doesn't std::thread work without calling std::cout?
I can't figure out why std::thread doesn't work for me without calling cout? How is that even possible?
/*
* Dispatcher
*/
void threadDispatcherF(FileReaderObject *dev) {
while(true) {
//print messages from bufer to console
string outpMsg = dev->getOutpMsg();
while(!(outpMsg.compare("") == 0)) {
//print message
std::cout << outpMsg;
outpMsg = dev->getOutpMsg();
}
usleep(dev->TIMEOUT_THREAD_US);
}
}
/*
* Initialisation of our reader
*/
void FileReaderObject::init() {
printMessage("File read device. Denis, 2018");
printMessage("Press q for exit");
std::thread threadDispatcher(threadDispatcherF, this);
threadDispatcher.join();
}
class FileReaderObject {
private:
const int TIMEOUT_THREAD_US = 10000;
const int TIMEOUT_WAIT_KEY_MS = 100;
mutex m_mutPrintMsg;
deque<string> m_consoleMsgs;
bool m_needStop;
int setReadParameters();
void printMessage(string msg);
//drive device
string getOutpMsg();
static string getChar();
void setNeedStop();
friend void threadRecvF (FileReaderObject *dev);
friend void threadSendF (FileReaderObject *dev);
friend void threadReadF (FileReaderObject *dev);
friend void threadDispatcherF(FileReaderObject *dev);
public:
FileReaderObject();
~FileReaderObject();
void init();
};
Answer the question
In order to leave comments, you need to log in
The optimizer is quite right to throw out code that does not change anything in the outside world.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question