The thread has exited with code 1: Join() and Detach()

The thread has exited with code 1: Join() and Detach()

I developing a software that needs to download several images and do some processing with opencv. I'm using curllib to download the images.So basically I get the images from different network cameras. What I want to do is download these images simultaneously, so I'm trying to use detach() and call the the curllib. The main reason I'm trying to use detach() is to do not wait for some download that can take longer or timeout. So I want to process what is available in the main loop. When I use detach(), after while the program stops without any error or exeption just showing:

I developing a software that needs to download several images and do some processing with opencv. I'm using curllib to download the images.So basically I get the images from different network cameras. What I want to do is download these images simultaneously, so I'm trying to use detach() and call the the curllib. The main reason I'm trying to use detach() is to do not wait for some download that can take longer or timeout. So I want to process what is available in the main loop. When I use detach(), after while the program stops without any error or exeption just showing:

The thread .... has exited with code 1 

The thread ....has exited with code 1

and after:

The program.... has exited with code 1....

No exceptions, no errors, nothing. just it.

If I change to join() it never stops. But with join I need to wait all threads download all images or timeout.

I will show an example with two requisitions:

//create class
ProcessImage* camera1 = new ProcessImage;
ProcessImage* camera2 = new ProcessImage;

for (;;) {

/* Must initialize libcurl before any threads are started */ curl_global_init(CURL_GLOBAL_ALL);

// Load Image into Class variable std::thread th(&ProcessImage::loadimage, camera1); if (th.joinable()) { th.detach(); }

std::thread th2(&ProcessImage::loadimage, camera2); if (th2.joinable()) { th2.detach(); }

// Process with Opencv camera1->run(); camera2->run();

}

My load image function:

void ProcessImage::loadimage() {

Mat loading;

//insert user and password
string password = ConfigData.username + ":" + ConfigData.password;

// Loading image with Curl
loading = curlImgClass(ConfigData.urlSnapshot.c_str(), password.c_str(), 5);


lastfail = false;

// Internal variable to store the downloaded image 
img = loading.clone();


if (loading.empty()) {

    lastfail = true;
    endoperation = true;

}

if (loading.total() < 500) {

    lastfail = true;
    endoperation = true;


}

}

Curl Functions:

size_t  ProcessImage::write_dataClass(char ptr, size_t size, size_t nmemb, void *userdata)
{
    vector<uchar> *stream = (vector<uchar>)userdata;
    size_t count = size * nmemb;
    stream->insert(stream->end(), ptr, ptr + count);
    return count;
}

cv::Mat ProcessImage::curlImgClass(const char *img_url, string userpass, int timeout) {

vector&lt;uchar&gt; stream;
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, img_url); //the img url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); // pass the writefunction
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;stream); // pass the stream ptr to the writefunction
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5); // timeout if curl_easy hangs,
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);


if (userpass != ":") {
    const char * c = userpass.c_str();
    curl_easy_setopt(curl, CURLOPT_USERPWD, c);
}

CURLcode res = curl_easy_perform(curl); // start curl
curl_easy_cleanup(curl); // cleanup

/* Check for errors */
if (res != CURLE_OK) {

    //return imdecode(stream, -1); // 'keep-as-is'
    cv::Mat test(cv::Size(1, 1), CV_64FC1);
    return test; // 'keep-as-is'
}
else {
    return imdecode(stream, -1);
}

}

What can I do to make it work with detach()? I tried a lot of controls like std::lock_guard<std::mutex> but nothing works, the software keep quitting.

editing: the only variable I use in both function is the img :

int ProcessImage::run(){

    if ( (img.total() &lt; 500)  || (lastfail == true)) {

        failcount = failcount + 1;

            // create a black window
            Mat imagefail(480, 640, CV_8UC3, Scalar(0, 0, 0));

           // putText(imagefail, "Connection Fail", cvPoint(480 / 2, 480 / 2), CV_FONT_HERSHEY_COMPLEX_SMALL, 1, CV_RGB(255, 255, 255), 1, 8, false);

            imshow(ConfigData.name.c_str(), imagefail);                
            waitKey(20);

            m_lock2 = false;

            std::cout &lt;&lt; "connection fail \n";

            return -1; // load fail
        //}

    }
    else {

        failcount = 0;
    }



    Mat sendimage = img.clone();
    Mat opacity = sendimage.clone();

   //// do other stuff with sendimage now ////

}


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...