vendredi 15 juillet 2022

How to wrap a c++ class (has a thread attribute) with Python using Pybind11?

I have a C++ class with an attribute thread and I want to wrap it in Python using Pybind11, the class looks like this : MyClass.hpp

class MyClass
{
    public:
        MyClass();
        ~MyClass();
        void start();    // start the thread
        void stop();    // stop the thread
    
        void mythread_function();   // the job done by the thread
        
        std::thread mythread;    // the thread attribute
    
};

MyClass.cpp

#include "MyClass.hpp"
#include <thread>

MyClass::MyClass()
{
    std::cout << "MyClass constructor" << std::endl;
}

MyClass::~MyClass()
{
    std::cout << "MyClass destructor" << std::endl;
}

void MyClass::start()
{
    mythread = std::thread(&MyClass::mythread_function, this);
}

void MyClass::stop()
{
    mythread.join();
}

void MyClass::mythread_function()
{
    for(int i = 0;i<5;i++){
        std :: cout << i << std::endl;
    }
}

Wrapper.cpp

#include <pybind11/pybind11.h>
#include "MyClass.hpp"

namespace py = pybind11;

PYBIND11_MODULE(mymodule, m){
    
    py::class_<MyClass>(m, "MyClass") 
            .def(py::init<>())
            .def("start", &MyClass::start) 
            .def("stop", &MyClass::stop)
            
            .def("mythread_function",&MyClass::mythread_function)
            
            .def_readwrite("mythread",&MyClass::mythread);
}

Command to compile the code :

g++ -shared -lpthread -fPIC -std=c++11 -I./pybind11/include/ `python3.7 -m pybind11 --includes` Wrapper.cpp MyClass.cpp -o mymodule.so `python3.7-config --ldflags`

The error :

/home/pi/.local/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) [with C = MyClass; D = std::thread; Extra = {}; type_ = MyClass; options = {}]’:
Wrapper.cpp:15:57:   required from here
/home/pi/.local/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:1676:56: error: use of deleted function ‘std::thread& std::thread::operator=(const std::thread&)’
             fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
                                                  ~~~~~~^~~~~~~
In file included from MyClass.hpp:1,
                 from Wrapper.cpp:2:
/usr/include/c++/8/thread:148:13: note: declared here
     thread& operator=(const thread&) = delete;
             ^~~~~~~~
In file included from Wrapper.cpp:1:
/home/pi/.local/lib/python3.7/site-packages/pybind11/include/pybind11/pybind11.h:99:5: error: ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) [with C = MyClass; D = std::thread; Extra = {}; type_ = MyClass; options = {}]::<lambda(pybind11::class_<MyClass>::type&, const std::thread&)>; Extra = {pybind11::is_method}; <template-parameter-1-3> = void]’, declared using local type ‘pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) [with C = MyClass; D = std::thread; Extra = {}; type_ = MyClass; options = {}]::<lambda(pybind11::class_<MyClass>::type&, const std::thread&)>’, is used but never defined [-fpermissive]
     cpp_function(Func &&f, const Extra &...extra) {

Can you please help me out? I have no idea how can I get rid of the error use of deleted function.

Aucun commentaire:

Enregistrer un commentaire