繁体   English   中英

如何停止C ++中的线程执行

[英]How to stop the thread execution in C++

我在主程序中创建了一个线程,一旦主程序终止,线程执行必须停止。 我正在使用reader.join(); 终止线程执行。 但这并没有停止执行。

我尝试使用下面提到的代码,我正在使用thread.join(); 函数,但无法终止线程。 在主程序之后,我的线程也继续执行。

#include <algorithm>
#include <array>
#include <atomic>
#include <mutex>
#include <queue>
#include <cstdint>
#include <thread>
#include <vector>

using namespace std;
using namespace std::chrono;

typedef pair<int, Mat> pairImage;

class PairComp {
public:
    bool operator()(const pairImage& n1, const pairImage& n2) const
    {
        if (n1.first == n2.first)
            return n1.first > n2.first;
        return n1.first > n2.first;
    }
};

int main(int argc, char* argv[])
{
    mutex mtxQueueInput;
    queue<pairImage> queueInput;
    int total = 0;
    atomic<bool> bReading(true);
    thread reader([&]() {
        int idxInputImage = 0;
        while (true) {
            Mat img = imread("img_folder/");
            mtxQueueInput.lock();
            queueInput.push(make_pair(idxInputImage++, img));
            if (queueInput.size() >= 100) {
                mtxQueueInput.unlock();
                cout << "[Warning]input queue size is " << queueInput.size();
                // Sleep for a moment
                sleep(2);
            }
            else {
                mtxQueueInput.unlock();
            }
        }
        bReading.store(false);
    });

    while (true) {
        pair<int, Mat> pairIndexImage;
        mtxQueueInput.lock();
        if (queueInput.empty()) {
            mtxQueueInput.unlock();
            if (bReading.load())
                continue;
            else
                break;
        }
        else {
            // Get an image from input queue
            pairIndexImage = queueInput.front();
            queueInput.pop();
        }
        mtxQueueInput.unlock();
        cv::Mat frame = pairIndexImage.second;

        cv::rectangle(frame, cv::Rect{ 100, 100, 100, 100 }, 0xff);
    }

    cv::imshow("out_image", frame);
    waitKey(1);

    if (total++ == 200)
        break;

    if (reader.joinable()) {
        reader.join();
    }

    return 0;
}

thread.join()不会导致线程终止,它会一直等到线程结束。 结束执行是线程的责任,例如,通过定期检查特定条件(例如标志)来结束线程的执行。

您已经有一个atomic标记bReading ,它似乎导致线程退出。

        if (queueInput.empty()) {
            mtxQueueInput.unlock();
            if (bReading.load())
                continue;
            else
                break;  // thread will exit when queue is empty and bReading == false

因此,您需要做的就是在调用thread.join()之前在外线程中设置bReading = false

bReading = false;
reader.join();

请注意, bReading.store(false); 线程中没有任何作用。


注意:不需要调用atomic.load()atomic.store() ,只需在代码中使用它们即可,它们将隐式调用load()store()

我不知道停止thread的内置可能性。 由于您的线程中嵌入了endless-loop ,因此它不会随时停止。

std::thread::join不会终止您的线程。 当需要时,您必须实现一些东西来结束循环。

  • thread必须退出时,将bool变量设置为false 例如while(run)或类似的东西; 为了简单起见,您还可以使用std::atomic<bool>
  • 您检查的信令变量。 std::condition_variable

您现在所要做的是,在main-thread中等待thread终止。 由于std::thread::join不会终止您的thread ,因此您的main-thread将永远执行。

注意:当您选择实施bool解决方案时。 您应该使用mutex或类似方法保护此bool

感谢您的评论。 因为我不想指出每个人都可以boost ,但是您提到了它。 在此处查找信息。

这个问题是不是与join其中(BTW)并不意味着可以用来停止或终止线程。

您的线程正在执行的函数包含一个while(true) ,它将永远不会终止,因为它只能休眠并解锁锁,没有别的。

这意味着将永远不会调用bReading.store ,因此在主线程循环中,您总是会经过

if (bReading.load())
        continue;

意思是main也将永远执行。


std::join用于从线程中等待另一个线程完成其工作。 当你做thread1.join()main线程什么情况是, main等到 thread1已完成执行任何其他指令之前执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM