K
K
Kamikaze10242018-03-10 02:07:17
C++ / C#
Kamikaze1024, 2018-03-10 02:07:17

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

2 answer(s)
A
Armenian Radio, 2018-03-10
@gbg

The optimizer is quite right to throw out code that does not change anything in the outside world.

J
jcmvbkbc, 2018-03-10
@jcmvbkbc

I can't figure out why std::thread doesn't work for me without calling cout? How is that even possible?

And in the assembler , then do not look in any way? I see that everything works out, what with cout, what without, what with -O0, what with -O2.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question