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 ////

}


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

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

Originally published by Maourice Gonzalez at https://www.onmsft.com

C++ is an incredibly fast and efficient programming language. Its versatility knows no bounds and its maturity ensures support and reliability are second to none. Code developed in C++ is also extremely portable, all major operating systems support it. Many developers begin their coding journey with the language, and this is no coincidence. Being object-oriented means it does a very good job of teaching concepts like classes, inheritance, abstraction, encapsulation and polymorphism. Its concepts and syntax can be found in modern languages like C#, Java and Rust. It provides a great foundation that serves as a high speed on ramp to the more popular, easier to use and modern alternatives.

Now it’s not all rosy. C++ has a very steep learning curve and requires developers to apply best practices to the letter or risk ending up with unsafe and/or poor performing code. The small footprint of the standard library, while most times considered a benefit, also adds to the level of difficulty. This means successfully using C++ to create useful complex libraries and applications can be challenging. There is also very little offered in terms of memory management, developers must do this themselves. Novice programmers could end up with debugging nightmares as their lack of experience leads to memory corruption and other sticky situations. This last point has lead many companies to explore fast performing, safe and equally powerful alternatives to C++. For today’s Microsoft that means Rust.

The majority of vulnerabilities fixed and with a CVE [Common Vulnerabilities and Exposures] assigned are caused by developers inadvertently inserting memory corruption bugs into their C and C++ code - Gavin Thomas, Microsoft Security Response Center

Rust began as a personal project by a Mozilla employee named Graydon Hoare sometime in 2006. This ambitious project was in pre-release development for almost a decade, finally launching version 1.0 in May 2015. In what seems to be the blink of an eye it has stolen the hearts of hordes of developers going as far as being voted the most loved language four years straight since 2016 in the Stack Overflow Developer Survey.

The hard work has definitely paid off. The end result is very efficient language which is characteristically object oriented. The fact that it was designed to be syntactically similar to C++ makes it very easy to approach. But unlike the aforementioned it was also designed to be memory safe while also employing a form of memory management without the explicit use of garbage collection.

The ugly truth is software development is very much a trial and error endeavor. With that said Rust has gone above and beyond to help us debug our code. The compiler produces extremely intuitive and user friendly error messages along with great direct linking to relevant documentation to aid with troubleshooting. This means if the problem is not evident, most times the answer is a click away. I’ve found myself rarely having to fire up my browser to look for solutions outside of what the Rust compiler offers in terms of explanation and documentation.

Rust does not have a garbage collector but most times still allocates and release memory for you. It’s also designed to be memory safe, unlike C++ which very easily lets you get into trouble with dangling pointers and data races. In contrast Rust employs concepts which help you prevent and avoid such issues.

There are many other factors which have steered me away from C++ and onto Rust. But to be honest it has nothing to do with all the great stuff we’ve just explored. I came to Rust on a journey that began with WebAssembly. What started with me looking for a more efficient alternative to JavaScript for the web turned into figuring out just how powerful Rust turns out to be. From its seamless interop…

Automatically generate binding code between Rust, WebAssembly, and JavaScript APIs. Take advantage of libraries like web-sys that provide pre-packaged bindings for the entire web platform. – Rust website

To how fast and predictable its performance is. Everything in our lives evolves. Our smartphones, our cars, our home appliances, our own bodies. C++ while still incredibly powerful, fast and versatile can only take us so far. There is no harm in exploring alternatives, especially one as exceptional and with as much promise as Rust.

What do you guys think? Have you or would you give Rust a try? Let us know your thoughts in the comments section below.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

Why you should move from Node.js to Rust in 2019

Rust Vs. Haskell: Which Language is Best for API Design?

7 reasons why you should learn Rust programming language in 2019

An introduction to Web Development with Rust for Node.js Developers

Why do C and C++ allow the expression (int) + 4*5?

Why is this&nbsp;<em>(adding a type with a value)</em>&nbsp;possible? (tried with g++ and gcc.)

(int) + 4*5;

Why is this (adding a type with a value) possible? (tried with g++ and gcc.)

I know that it doesn't make sense (and has no effect), but I want to know why this is possible.