使用在python对象中声明的c++std::mutex

2024-04-25 20:57:14 发布

您现在位置:Python中文网/ 问答频道 /正文

简短问题: 在使用malloc获取相应内存之后,如何初始化结构中的std::mutex?你知道吗

更多细节: 我正在尝试用一个后台线程创建一个python模块

我创建和运行线程没有任何问题,但是如果我尝试使用Python对象中存储的互斥锁,它就会崩溃。你知道吗

如果我在cpp文件的头部定义mutex,它就可以工作了。但我更愿意将其存储在设备结构中

在我的设备里

#include <Python.h>
#include "structmember.h"

typedef struct sSharedData
{
    bool mStop;
} sSharedData;

typedef struct sDevice
{
    PyObject_HEAD
    std::thread mDataThread;
    std::mutex mDataThreadMutex;
    sSharedData mDataThreadSharedData;
} sLeddarDevice;


PyObject *StartDataThread( sLeddarDevice *self, PyObject *args );
PyObject *StopDataThread( sLeddarDevice *self, PyObject *args );

static PyMethodDef Device_methods[] =
{
    { "StartDataThread", ( PyCFunction )StartDataThread, METH_NOARGS, "Start the thread." },
    { "StopDataThread", ( PyCFunction )StopDataThread, METH_NOARGS, "Stop the thread." },
    { NULL }  //Sentinel
};

static PyMemberDef Device_members[] =
{
    { NULL }  //Sentinel
};

static PyTypeObject LeddarDeviceType =
{
    PyObject_HEAD_INIT( NULL )
    0,                              //ob_size
    "LeddarPy.Device",              //tp_name
    sizeof( sDevice ),        //tp_basicsize
    0,                              //tp_itemsize
    ( destructor )Device_dealloc,   //tp_dealloc
    0,                              //tp_print
    0,                              //tp_getattr
    0,                              //tp_setattr
    0,                              //tp_compare
    0,                              //tp_repr
    0,                              //tp_as_number
    0,                              //tp_as_sequence
    0,                              //tp_as_mapping
    0,                              //tp_hash
    0,                              //tp_call
    0,                              //tp_str
    0,                              //tp_getattro
    0,                              //tp_setattro
    0,                              //tp_as_buffer
    Py_TPFLAGS_DEFAULT,             //tp_flags
    "Device object.",               // tp_doc
    0,                              //tp_traverse
    0,                              //tp_clear
    0,                              //tp_richcompare
    0,                              //tp_weaklistoffset
    0,                              //tp_iter
    0,                              //tp_iternext
    Device_methods,                 //tp_methods
    Device_members,                 //tp_members
    0,                              //tp_getset
    0,                              //tp_base
    0,                              //tp_dict
    0,                              //tp_descr_get
    0,                              //tp_descr_set
    0,                              //tp_dictoffset
    0,                              //tp_init
    0,                              //tp_alloc
    Device_new,                     //tp_new
};

在我的心里PyDevice.cpp文件你知道吗

#include "LeddarPyDevice.h"
//Constructor
PyObject *Device_new( PyTypeObject *type, PyObject *args, PyObject *kwds )
{
    sLeddarDevice *self;

    self = ( sLeddarDevice * )type->tp_alloc( type, 0 );

    if( self != nullptr )
    {
        self->mDataThreadSharedData.mStop = false;
    }

    return ( PyObject * )self;
}

//Destructor
void Device_dealloc( sLeddarDevice *self )
{
    DebugTrace( "Destructing device." );
    Py_TYPE( self )->tp_free( ( PyObject * )self );
}

PyObject *StartDataThread( sLeddarDevice *self, PyObject *args )
{
    DebugTrace( "Starting thread" );
    self->mDataThreadMutex.lock();
    self->mDataThreadSharedData.mStop = false;
    self->mDataThreadMutex.unlock();
    self->mDataThread = std::thread( DataThread, self );

    Py_RETURN_TRUE;
}

每当我尝试使用self->mDataThreadMutex.lock()时,它就会崩溃。你知道吗

我不确定互斥锁是否正确初始化,我更习惯于pthread互斥锁,您需要手动初始化它。你知道吗


Tags: selfincludedeviceasargsthreadpyobjectstd
1条回答
网友
1楼 · 发布于 2024-04-25 20:57:14

Python是用C编写的,因此它不会为您运行C++构造函数/析构函数。没有它们std::mutex只是一个毫无意义的结构,使用时会崩溃。您需要的是placement new某处(在分配self之后?)地址:

self = ( sLeddarDevice * )type->tp_alloc( type, 0 );
new (self) sLeddarDevice {};

第二行在self上调用sLeddarDevice的构造函数,而不分配内存。该构造函数也将调用每个成员的构造函数。你知道吗

完成后还必须手动调用析构函数:self->~sLeddarDevice();

当C++与Python相结合时,你应该做<强>永远>强。否则,在最佳情况下,您将泄漏内存。在更糟的情况下,遇到随机碰撞的未定义行为。你知道吗

相关问题 更多 >