阻塞执行程序,直到回调函数完成执行

时间:2019-04-29 09:44:19

标签: c++ qt

如何阻止程序执行,直到回调函数完成执行?

从我的main()我启动interface.getImage();谁想要从我们的数据库中获取图像的功能。当我们收到图像时,回调函数void InterfaceColliseo :: dataReceived(std :: shared_ptr数据)开始执行。 但是我有一个问题,我的程序main()在执行回调函数之前终止?

main.cpp

int main(){
        InterfaceColliseo interface;
        IMAGE = true;
        interface.getImage();

        // want to block program here
        return 0;
}


回调功能

void InterfaceColliseo::dataReceived (std::shared_ptr<IData> data)
{
    if (!data->isValid())
        return;

    const unsigned char* twoDImageData = data->get2DImageData();
    int width2DImageData = data->getWidth2DImageData();
    int height2DImageData = data->getHeight2DImageData();
    const unsigned char* disparityData = data->getDisparityData();
    int widthDisparityData = data->getWidthDisparityData();
    int heightDisparityData = data->getHeightDisparityData();

    if(IMAGE) {
        saveImage(twoDImageData, width2DImageData, height2DImageData,
                  disparityData, widthDisparityData, heightDisparityData);
        IMAGE = false;
    }

    if(ACQUISATION){
        QList<GstObservationBasic> detectorData = data->getObstaclesData();
        getObstacles(detectorData);

    }

}

2 个答案:

答案 0 :(得分:0)

我认为您只能使用std中的线程。当您使用join时,主线程将等待,直到joined线程完成其工作为止。

#include <thread>

//in main
std::thread myThread(interface.getImage);
myThread.join();

答案 1 :(得分:0)

#include "interface_colliseo.h"

std::mutex mtx;
std::condition_variable cv;


bool IMAGE;
bool ACQUISATION;

InterfaceColliseo::InterfaceColliseo(){
}

void InterfaceColliseo::startStreaming(){
    dataReceiver = _device->getDataReceiver();
    start();

}

void InterfaceColliseo::getImage(){
    dataReceiver = _device->getDataReceiver();
    start();
}

InterfaceColliseo::InterfaceColliseo(QString IP): _IP(IP) {
    qDebug() << "INDUS-5: IP server: " << _IP;
    qDebug() << "INDUS-5: Connecting to sensor...";
    _colliseoClient.setIPServer(_IP);

}

bool InterfaceColliseo::connect2UT(){
    QString path = qApp->applicationDirPath()+"/Configuration.ini";
    QSettings config(path, QSettings::IniFormat);
    _IP = config.value("Connection/IP","10.0.3.144").toString();

    _colliseoClient.setIPServer(_IP);
    _device = _colliseoClient.getDevice();
    _device->connectSensor();
    bool connect = _device->isConnected();
    return connect;
}

QString InterfaceColliseo::sensorName(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getOrderNumber();
}

QString InterfaceColliseo::sensorFirmwareVersion(){
    return _device->getDeviceDiagnostics()->getOperatingData()
           ->getDeviceInformation()->getFirmwareVersion();
}

QString InterfaceColliseo::getSensorHeadPN(QString sensor){
     return _device->getDeviceDiagnostics()->getOperatingData()
             ->getDeviceInformation()->getSensorHeadPN(sensor);
}

QString InterfaceColliseo::getEvaluationUnitSN(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getEvaluationUnitSN();
}

QString InterfaceColliseo::getEvaluationUnitPN(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getEvaluationUnitPN();
}

QString InterfaceColliseo::getEvaluationUnitFirmwareVersion(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getEvaluationUnitFirmwareVersion();

}

QString InterfaceColliseo::getEstimatedAngleX(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getEstimatedAngleX();

}

QString InterfaceColliseo::getEstimatedAngleZ(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getEstimatedAngleZ();

}

QString InterfaceColliseo::getEstimatedHeight(){
    return _device->getDeviceDiagnostics()->getOperatingData()
            ->getDeviceInformation()->getEstimatedHeight();

}

void InterfaceColliseo::saveImage(const unsigned char*twoDImageData,
                                  int width2DImageData, int height2DImageData,
                                  const unsigned char*disparityData,
                                  int widthDisparityData, int disptHeight){
    Configuration configuration;

    QString logFolder = configuration.getLogFolder();

    QImage imgRight(twoDImageData, width2DImageData, height2DImageData, QImage::Format_Indexed8);
    QImage imgDisparity(disparityData, widthDisparityData, disptHeight, QImage::Format_Indexed8);

    QPixmap imgRght = QPixmap::fromImage(imgRight);
    QPixmap imgDisp = QPixmap::fromImage(imgDisparity);

    QString rghtImgPath = logFolder + "raw_image.png";
    QString dispImgPath = logFolder + "disp_image.png";

    imgRght.save(rghtImgPath, "PNG");
    imgDisp.save(dispImgPath, "PNG");

}


void InterfaceColliseo::getObstacles(QList<GstObservationBasic> detectorData){

    if (detectorData.size() == 0)
    {
        qDebug() << "Obstacles: no detected obstacles.";
        return;
    }

    Configuration config;
    config.writeLog("***************Obstacles List Acquisation******************");
    Q_FOREACH(const GstObservationBasic &obs, detectorData)
    {

        qDebug() << "Obstacles: " << gstObservationToString(obs);
        config.writeLog(gstObservationToString(obs));
    }
}


void InterfaceColliseo::dataReceived (std::shared_ptr<IData> data)
{
    if (!data->isValid())
        return;

    const unsigned char* twoDImageData = data->get2DImageData();
    int width2DImageData = data->getWidth2DImageData();
    int height2DImageData = data->getHeight2DImageData();
    const unsigned char* disparityData = data->getDisparityData();
    int widthDisparityData = data->getWidthDisparityData();
    int heightDisparityData = data->getHeightDisparityData();

    if(IMAGE) {
        saveImage(twoDImageData, width2DImageData, height2DImageData,
                  disparityData, widthDisparityData, heightDisparityData);
        IMAGE = false;
    }

    if(ACQUISATION){
        QList<GstObservationBasic> detectorData = data->getObstaclesData();
        getObstacles(detectorData);
        ACQUISATION = false;

    }

}

void InterfaceColliseo::start() {

    dataReceiver->addListener(this);

    if(dataReceiver->isListening())
        dataReceiver->stopListening();

    dataReceiver->startListening();
    _device->triggerSingleImageAcquisition();
}