lundi 25 février 2019

Joining threads while exception is thrown

I am using the C++ 11 std::thread library. Is it valid to join a thread in a destructor while an exception is thrown? The code I have is:

#include <thread>
#include <stdio.h>
#include <exception>
#include <unistd.h>

class the_thread {
private:
    std::thread t;
    bool abort;
public:
    the_thread();
    ~the_thread();

    void run();
};

the_thread::the_thread()
{
    abort = false;
    t = std::thread(&the_thread::run, this);
#if 0
    printf("before detach ...\n");
    t.detach();
    printf("after detach ...\n");
#endif
}

the_thread::~the_thread()
{
    abort = true;

printf("destruct thread container\n");
    if (t.joinable()) {
printf("into join\n");
        t.join();
printf("out of join\n");
    }
}

void the_thread::run()
{
    int i;

    i=0;
    while (!abort) {
        printf("hallo %d\n", i);
        i++;
        std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    }
}

int run_thread_and_throw_exception(void)
{
    the_thread t;

    sleep(5);
    throw std::runtime_error("some expected exception");
}


int main(int argc, char ** argv)
{
    try {
        run_thread_and_throw_exception();
    } catch (const std::runtime_error &e) {
        printf("exception %s caught\n", e.what());
    }

    return 0;
}

When compiling and running with

gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

on my Ubuntu 16.04 development machine, this code behaves as expected:

bash$ ./thread_test 
hallo 0
hallo 1
destruct thread container
into join
out of join
exception some expected exception caught
bash$

However when compiling with

arm-linux-gnueabi-g++ -Wall -g -O2 -std=c++11   -c -o main.o main.cpp
arm-linux-gnueabi-g++ -Wall -g -O2 -std=c++11 main.o -o thread_test -lpthread -static

(we need the -static because there is no C++ 11 library on the target) and running it on the target, strange things happen:

bash-on-target# ./thread_test
hallo 0
hallo 1
destruct thread container
into join
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error 1082172112
Aborted

So it seems that the join in the destructor fails for some reason. Furthermore when trying to detach the thread, the detach fails rightaway:

bash-on-target# ./thread_test
before detach ...
terminate called without an active exception
hallo 0
hallo 0
Aborted

So to repeat my question: is joining threads (and possibly sleeping) while an exception is thrown valid in C++11? Is it possible that there is a bug in the C++ 11 library for ARM that comes with Ubuntu 16.04?

The ARM toolchain is:

bash$ arm-linux-gnueabi-g++ --version
arm-linux-gnueabi-g++ (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Thanks and best wishes, Johannes

Aucun commentaire:

Enregistrer un commentaire