cauchi
cauchi

Reputation: 1543

How to use boost::thread::at_thread_exit or call a function when thread is done

This is a minimal code to illustrate what I need. It doesn't work, because (as rightly the error message says when compiling) at_thread_exit is not a member of boost::thread. I know is related to the namespace this_thread, I've been going through the documentation at the boost page, but cannot follow how to use at_thread_exit. I haven't been able to find any simple example of how to use it using google.

#include <boost/thread.hpp>
#include <iostream>

class A{
    public:
    void callme(){
        int a = 1;
    }
    void runThread()  {
        boost::thread td(&A::callme,this);
        td.at_thread_exit(&A::done,this);
        td.join();
    }
    void done() {
        std::cout << "I am done!!!\n";
    }
};

int main(int argc, char **argv) {
    A *a = new A();
    a->runThread();
    delete a;
    return EXIT_SUCCESS;
}

Upvotes: 0

Views: 1046

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275385

boost::thread td([this]{
  callme();
  done();
});

at_thread_exit only works within the same thread; it would require sychronization otherwise, and that would make every thread pay for it when only some threads use it.

Upvotes: 2

Related Questions