【发布时间】:2015-01-30 00:34:37
【问题描述】:
我有一个任务需要在 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 的单元格矩阵,其中每个元素都包含一个 1x1 双精度矩阵。
使用 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 造成的,但我不知道如何解决这个问题,因为我必须在运行中创建这些数组(我不知道例如,总是提前知道创建它们的大小)。
【问题讨论】:
标签: c multithreading matlab pthreads mex