Mar 31, 2015

[C++11/14] gap between exit releasing resource and other thread still running

Darn python bug while calling exit in signal handler.


#include <iostream>
#include <thread>

#include <signal.h>

using namespace std;


void handler(int)
{
    cout << "boom~" << endl;
    exit(42);
}


void _set_handler(int sig)
{
    struct sigaction sact;
    sact.sa_handler = &handler;
    sigaction(sig, &sact, nullptr);
}


template<typename... I>
void set_handler(I... signals)
{
   [](...){}( (_set_handler(signals),0)... );
}


struct ThisIsAType
{
    int a_{42};

    ~ThisIsAType()
    {
        a_ = 0;
        cout << "Destructor fires" << endl;
    }
};


ThisIsAType a_type{};


void runme()
{
    while(true)
    {
        cout << a_type.a_ << endl;
    }
}


int main()
{
    set_handler(SIGTERM, SIGINT);

    thread t1{runme};
    t1.detach();

    while(true)
        continue;
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.