首页 文章

多线程(pthreads)MATLAB mex函数导致MATLAB在退出后崩溃

提问于
浏览
3

我有一个任务需要在MATLAB中多次执行,我想通过使用多线程MEX函数来减少执行任务所花费的时间 . 但是,由于双重释放或损坏错误,MEX函数导致MATLAB在退出后崩溃,因此我遇到了一些麻烦 .

任务本身非常大,但我能够使用以下最小(非)工作示例重现错误:

#include <pthread.h>
#include "mex.h"
#include "matrix.h"

/* hard coded number of matrices to create per thread */
int num_mat = 10;

/* struct for passing information to the thread */
typedef struct {
    int pnum;
    int num_mat;
    mxArray *outMatrix;
} pt_info_t;


/* thread function */
void *doThread(void *tinfo){

    /* extract info from the struct */
    pt_info_t pinfo = *((pt_info_t *)tinfo);
    mxArray *outMatrix = pinfo.outMatrix;

    /* create a cell matrix */
    mxArray *one_cell = mxCreateCellMatrix(pinfo.num_mat, 1);
    int i = 0;
    for(i = 0; i < pinfo.num_mat; i++){
        /* fill the cell matrix with 1x1 double matrices */
        mxArray *one_mat = mxCreateDoubleMatrix(1,1,mxREAL);
        mxSetCell(one_cell, i, one_mat);
    }

    /* add the cell matrix to the return cell matrix */
    mxSetCell(outMatrix, pinfo.pnum, one_cell);

    /* exit the thread */
    pthread_exit(NULL);
}

/* thread entry function */
void t_comp_routine(int num_threads, mxArray *outMatrix){

    /* thread setup */
    pthread_t threads[num_threads];
    pt_info_t pinfo[num_threads];

    /* add information to the thread info structs and start threads */
    int pnum = 0;
    for(pnum = 0; pnum < num_threads; pnum++){
        pinfo[pnum].pnum = pnum;
        pinfo[pnum].num_mat = num_mat;
        pinfo[pnum].outMatrix = outMatrix;

        pthread_create(&threads[pnum], NULL, doThread, (void *)(pinfo + pnum));
    }

    /* join the threads */
    for(pnum = 0; pnum < num_threads; pnum++){
        pthread_join(threads[pnum], NULL);
    }


}

/* same routine but without threads */
void comp_routine(int num_pretend_threads, mxArray *outMatrix){

    int pnum = 0;
    for(pnum = 0; pnum < num_pretend_threads; pnum++){
        mxArray *one_cell = mxCreateCellMatrix(num_mat, 1);

        int i = 0;
        for(i = 0; i < num_mat; i++){
            mxArray *one_mat = mxCreateDoubleMatrix(1,1,mxREAL);
            mxSetCell(one_cell, i, one_mat);
        }

        mxSetCell(outMatrix, pnum, one_cell);
    }
}

/* The gateway function 
* nlhs = NUM left hand side args (OUTPUT)
* nrhs = NUM rhs args (INPUT)
* plhs = array of output args (OUTPUT)
* prhs = array of input args (INPUT)
*/
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{    
    int num_threads = mxGetScalar(prhs[0]);
    mxArray *outMatrix = mxCreateCellMatrix(num_threads, 1);

    /* t_comp_routine causes matlab to crash after exiting */
    t_comp_routine(num_threads, outMatrix);

    /* comp_routine does not */
    /* comp_routine(num_threads, outMatrix); */

    plhs[0] = outMatrix;
    printf("Finished!\n");
}

该程序接收一个 num_threads 变量,并输出一个单元数组,其元素数与线程数相同 . 输出阵列的每个单元是长度为10的单元矩阵,其中每个单元包含1×1双矩阵 .

使用10个线程调用该函数会导致以下结果:

>> thread_test(10)
Finished!
ans = 
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
    {10x1 cell}
*** glibc detected *** /usr/local/MATLAB/R2013a/bin/glnxa64/MATLAB: double free or corruption (fasttop): 0x00002b89a000cf10 ***

我为了简洁而排除了Backtrace,但如果需要,我非常乐意提供它 .

偶尔,调用少于10个线程的函数将完成而不会显示错误,但我怀疑可能仍然存在问题 . 非线程函数 comp_routine 执行相同的任务并且多次运行且没有错误 .

我相信错误可能是在线程函数内部创建mxArrays的结果,但我不知道如何解决这个问题,因为我需要动态创建这些数组的完整程序(我并不总是知道未来例如,创建它们的时间大小) .

1 回答

  • 4

    MEX API(包括 mx* 函数)是NOT thread safe (MathWorks Support Team) . 您只能从主MATLAB线程调用mx * / mex *函数,但有一些例外(参见下文) . 在使用 pthread_create 启动的 doThread 中,您调用了几个不能在那里使用的函数:

    • mxCreateCellMatrix

    • mxCreateDoubleMatrix

    • mxSetCell

    您必须将普通旧数据传入和传出 doThread ,并在使用 pthread_create 调用的线程之外管理单元阵列 . 除了演示使用Windows API进行多线程处理的example from MathWorks之外,Dirk-Jan Kroon还提供了另一个使用Pthread和Windows API多线程处理MEX函数的好例子,其中包括his tutorial on the File Exchangehis non-rigid registration submission等 . 使用MexThread submission中的 std::thread 也可能值得查看C 11方法 .

    例外情况(YMMV):mexErrMsgIdAndTxt .

相关问题