C++ - How do I revive a thread with boost?

落爺英雄遲暮 提交于 2020-01-17 07:54:48

问题


SUMMARY: Client is in Teamspeak server with other users. When other users begin speaking, this plugin runs a function setSpeakerPosition() on the user every 500ms until they stop speaking. It should do it again if they start speaking again.

PROBLEM: If I simply do a .reset(new boost::thread(...)) every time they start speaking, it seems to overwrite an existing thread (if they've spoken before) and leads to a crash. I believe I need to 'revive' an existing thread, or kill it properly.

// Create an array like: speakingThreads[client_id] = array(bIsSpeaking, ptrThread)
typedef boost::shared_ptr<boost::thread> SmartThread;
map<anyID, pair<bool, SmartThread>> speakingThreads;

// Called when someone starts or stops talking
void ts3plugin_onTalkStatusChangeEvent(uint64 serverConnectionHandlerID, int status, int isReceivedWhisper, anyID clientID) {

    // Get my client id
    anyID myID;
    ts3Functions.getClientID(serverConnectionHandlerID, &myID);

    // If someone else starts talking
    if(clientID != myID && status == STATUS_TALKING) {
        // Set client to 'is speaking' (allows thread loop to run)
        speakingThreads[clientID].first = true;

        // If this is the first time they have spoken, start a thread
        if(speakingThreads[clientID].second == NULL) {
            // Create thread to keep updating speaker position while he's speaking
            speakingThreads[clientID].second.reset(new boost::thread(speakerActiveThread, serverConnectionHandlerID, clientID));
            _log("Starting Thread", 0);
        }
        // Or if they've spoken before, revive the thread
        else {
            speakingThreads[clientID].second->speakerActiveThread(serverConnectionHandlerID, clientID); // error C2039: 'speakerActiveThread' : is not a member of 'boost::thread'
            _log("Reviving Thread", 0);
        }
    }
    // If someone else stops talking
    else if(clientID != myID && status == STATUS_NOT_TALKING) {
        if(speakingThreads.find(clientID) != speakingThreads.end()) {
            // Disable thread loop for the speaker to stop updating his position
            speakingThreads[clientID].first = false;
            _log("Interrupted Speaking Thread", 0);
        }
    }
}

// Thread run when someone starts speaking, loops until they finish speaking
void speakerActiveThread(uint64 serverConnectionHandlerID, anyID clientID) {
    // While client 'is speaking'
    while(speakingThreads[clientID].first) {
        if(setSpeakerPosition(serverConnectionHandlerID, clientID) != ERROR_ok) {
            _log("ERROR Setting Speaker Position", 1);
        }
        _log("Set Speaker Position", 0);
        Sleep(UPDATE_SPEAKER_MS); // 500
    }
}

回答1:


You can not "revive" threads. After you've created a boost::thread object, passing to it a thread function you want to execute, the function runs in a separate OS thread; when the function ends - the thread ends (exits). Note that if boost::thread object gets destroyed (eg., goes out of scope), it doesn't affect the thread itself.



来源:https://stackoverflow.com/questions/9475163/c-how-do-i-revive-a-thread-with-boost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!