Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have an event driven system that needs the ability to turn on and off a logger. The issues is that the user could spam a button which could try to turn on the logger over and over again. The turning on and off will only happen on the main thread, and the () operator is where the worker thread is run. Will the following code prevent me from locking up the main thread via a button spam?

void LoggerThread::Pause() {
    if (mPauseMutex.try_lock()) {
        mPauseMutex.unlock();
    } else {
        return;
    }

    boost::unique_lock<boost::mutex> lock(mPauseMutex);
    mPause = true;
}

void LoggerThread::Resume(uint32_t setNumber_) {
    if (mPauseMutex.try_lock()) {
        mPauseMutex.unlock();
    } else {
        return;
    }

    boost::unique_lock<boost::mutex> lock(mPauseMutex);
    mSetNumber = setNumber_;
    mPause = false;
    mPauseChanged.notify_one();
}

bool LoggerThread::operator ()() {
    std::vector<unsigned char> log;
    unsigned int x = 8;

    for (;;) {
        boost::mutex::scoped_lock lock(mPauseMutex);
        while(mPause) {
            mPauseChanged.wait(lock);
        }

        std::vector<unsigned char> tempLogSet;
        mLogQueue.wait_and_pop(tempLogSet);
        if(tempLogSet.size() > 8){
            x = 8;
            LoggerFileConnector("attempt.binary").Append(tempLogSet);
            while (x < tempLogSet.size()){
                int datalength = tempLogSet[x + 6];
                int loglength = 13 + datalength;

                log.clear();
                std::copy(tempLogSet.begin() + x, tempLogSet.begin() + x +
                    loglength, std::back_inserter(log));

                Alert(log);

                x += loglength;
            }
        }
    }
    return true;
}
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.