Boost logo

Boost Users :

Subject: Re: [Boost-users] Creating a boost thread in a matlab mex file causes matlab to crash
From: bjs109 (ben.j.stephens_at_[hidden])
Date: 2009-06-25 04:51:31


All very logical steps, and steps which I'm sure I tried at some point ;)

Firstly, the posix threads solution works just fine, I've included the
program I tested (a slight modification of a standard example program).

/*
 * p_hello.c -- a hello program (in pthread)
 */
#include "mex.h"
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>

#define MAX_THREAD 1000

typedef struct {
        int id;
} parm;

void *hello(void *arg)
{
        parm *p=(parm *)arg;
        printf("Hello from node %d\n", p->id);
        return (NULL);
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
        int n,i;
        pthread_t *threads;
        pthread_attr_t pthread_custom_attr;
        parm *p;

        n=3;

        if ((n < 1) || (n > MAX_THREAD))
        {
                printf ("The no of thread should between 1 and
%d.\n",MAX_THREAD);
                exit(1);
        }

        threads=(pthread_t *)malloc(n*sizeof(*threads));
        pthread_attr_init(&pthread_custom_attr);

        p=(parm *)malloc(sizeof(parm)*n);
        /* Start up thread */

        for (i=0; i<n; i++)
        {
                p[i].id=i;
                pthread_create(&threads[i], &pthread_custom_attr, hello,
(void *)(p+i));
        }

        /* Synchronize the completion of each thread. */

        for (i=0; i<n; i++)
        {
                pthread_join(threads[i],NULL);
        }
        free(p);
}

I tried using boost version 1.38, I realize it's not quite the latest
version but it's the latest version easily available to me at the moment. I
used the application documented at the start of this thread. Once again, we
have a crash dump from matlab:

------------------------------------------------------------------------
       Segmentation violation detected at Thu Jun 25 09:37:20 2009
------------------------------------------------------------------------

Configuration:
  MATLAB Version: 7.7.0.471 (R2008b)
  MATLAB License: 166534
  Operating System: Linux 2.6.18-128.1.6.el5.centos.plusPAE #1 SMP Thu Apr 2
13:33:12 EDT 2009 i686
  GNU C Library: 2.5 stable
  Window System: No active display
  Current Visual: None
  Processor ID: x86 Family 6 Model 7 Stepping 10, GenuineIntel
  Virtual Machine: Java 1.6.0_04 with Sun Microsystems Inc. Java
HotSpot(TM) Client VM mixed mode
  Default Encoding: ISO-8859-1

Fault Count: 1

Register State:
  eax = 00000000 ebx = 0067eff4
  ecx = a830d4a4 edx = a830dbd8
  esi = 098d7dd0 edi = 098d7260
  ebp = 05a64168 esp = 05a64130
  eip = 0067149e flg = 00010202

Stack Trace:
  [0] libpthread.so.0:pthread_join~(0, 0, 0x098d7270, 0x08aeeae4 "¤)") + 30
bytes
  [1]
libboost_thread-gcc41-mt-1_34_1.so.1.34.1:boost::thread::join()~(0x098d7dd0,
0x098d7264, 0x098d7260, 0x05a641df) + 42 bytes
  [2] threadtest.mexglx:mexFunction~(0, 0x05a648fc, 0, 0x05a6495c) + 1266
bytes
  [3] libmex.so:mexRunMexFile(0, 0x05a648fc, 0, 0x05a6495c) + 111 bytes
  [4] libmex.so:Mfh_mex::runMexFileWithSignalProtection(int, mxArray_tag**,
int, mxArray_tag**)(0x098cc8f0 "HÑL", 0, 0x05a648fc, 0) + 119 bytes
  [5] libmex.so:Mfh_mex::dispatch_file(int, mxArray_tag**, int,
mxArray_tag**)(0x098cc8f0 "HÑL", 0, 0x05a648fc, 0) + 257 bytes
  [6] libmwm_dispatcher.so:Mfh_file::dispatch_fh(int, mxArray_tag**, int,
mxArray_tag**)(0x098cc8f0 "HÑL", 0, 0x05a648fc, 0) + 278 bytes
  [7] libmwm_interpreter.so:inDispatchFromStack(int, char const*, int,
int)(561, 0x098cb6bc "threadtest", 0, 0) + 1074 bytes
  [8] libmwm_interpreter.so:inDispatchCall(char const*, int, int, int, int*,
int*)(0, 0x05a64c18, 0x098d7ca8, 0x007c3170) + 160 bytes
  [9] libmwm_interpreter.so:.L718(1, 0, 1, 0) + 183 bytes
  [10] libmwm_interpreter.so:protected_inInterp(inDebugCheck, int, int,
opcodes, inPcodeNest_tag*, int*)(1, 0, 1, 0) + 120 bytes
  [11] libmwm_interpreter.so:inInterPcodeSJ(inDebugCheck, int, int, opcodes,
inPcodeNest_tag*, int*)(0, 0x098e2dc0, 0x05a6507c, 0x05a6507c) + 286 bytes
  [12] libmwm_interpreter.so:inExecuteMFunctionOrScript(Mfh_mp*,
bool)(0x098c7ef8, 1, 0x05a65ec8, 0) + 803 bytes
  [13] libmwm_interpreter.so:inRunMfile(int, mxArray_tag**, int,
mxArray_tag**, Mfh_mp*, inWorkSpace_tag*)(0, 0x05a65ec8, 0, 0) + 676 bytes
  [14] libmwm_interpreter.so:Mfh_mp::dispatch_file(_mdUnknown_workspace*,
int, mxArray_tag**, int, mxArray_tag**)(0x098c7ef8, 0, 0, 0x05a65ec8) + 64
bytes
  [15] libmwm_interpreter.so:Mfh_mp::dispatch_file(int, mxArray_tag**, int,
mxArray_tag**)(0x098c7ef8, 0, 0x05a65ec8, 0) + 56 bytes
  [16] libmwm_dispatcher.so:Mfh_file::dispatch_fh(int, mxArray_tag**, int,
mxArray_tag**)(0x098c7ef8, 0, 0x05a65ec8, 0) + 278 bytes
  [17] libmwm_interpreter.so:inEvalPcodeHeaderToWord(_memory_context*, int,
mxArray_tag**, _pcodeheader*, Mfh_mp*, unsigned int)(1954952, 0, 0x05a65ec8,
0x05a65d78) + 199 bytes
  [18] libmwm_interpreter.so:in_local_call_script_function(_memory_context*,
_pcodeheader*, int, mxArray_tag**, unsigned int, bool)(0x05a65ec8, 0, 0,
0x01031396) + 108 bytes
  [19] libmwm_interpreter.so:inEvalStringWithIsVarFcn(_memory_context*, char
const*, EvalType, int, mxArray_tag**, inDebugCheck, _pcodeheader*, int*,
bool (*)(void*, char const*), void*, bool, bool)(0, 0x05a65ec8, 0, 0) + 2884
bytes
  [20] libmwm_interpreter.so:InterpBridge::EvalCmdWithLocalReturn(char
const*, int*, bool, bool)(0x08bd6e48, 0x098beda0 "threadtest\n", 0, 0) + 151
bytes
  [21] libmwm_interpreter.so:inEvalCmdWithLocalReturn(0x098beda0
"threadtest\n", 0, 0, 1) + 74 bytes
  [22] libmwbridge.so:evalCommandWithLongjmpSafety(char const*)(0x098beda0
"threadtest\n", 1, 2746196, 0xb7fc9688) + 116 bytes
  [23] libmwbridge.so:mnParser(0x098dac00 "x1|", 0x092a2b70 "(öb", 1,
0x08be19b8) + 283 bytes
  [24] libmwmcr.so:mcrInstance::mnParser()(0x08be19f0, 0x05a66364,
0x08be19b8, 0x08be1a28) + 112 bytes
  [25] MATLAB:mcrMain(int, char const**)(1, 0xbfb602c4, 5, 0) + 538 bytes
  [26] libmwmcr.so:runMcrMain(void*)(0xbfb601e8, 0x05a66470 "ôïg",
0x05a66470 "ôïg", 0x05a66470 "ôïg") + 38 bytes
  [27] libpthread.so.0:0x0067049b(0x05a66b90, 0, 0, 0)

And using GDB I obtained the following stack dumps:

Thread 12
#0 0x0067149e in pthread_join () from /lib/libpthread.so.0
#1 0x0029055a in boost::thread::join (this=0x9540928) at
libs/thread/src/thread.cpp:226
#2 0x00d69296 in mexFunction () from
/home/ben/eclipse/ws-sandbox/SandboxWS/libASAMatlab/src/threadtest.mexglx
#3 0x0035b84f in mexRunMexFile () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libmex.so
#4 0x00358891 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libmex.so
#5 0x00359cfb in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/../../bin/glnx86/libmex.so
#6 0x0031b340 in Mfh_file::dispatch_fh () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_dispatcher.so
#7 0x00f37d5a in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#8 0x00ee5d84 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#9 0x00ee7127 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#10 0x00eea0de in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#11 0x00ee8f62 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#12 0x00ee99a9 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#13 0x00f4b7fe in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#14 0x00f24d30 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#15 0x00f23c20 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#16 0x0031b340 in Mfh_file::dispatch_fh () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_dispatcher.so
#17 0x00f2cb7d in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#18 0x00edc93c in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#19 0x00ee0ad0 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#20 0x00ee10c5 in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#21 0x00eaed28 in inEvalCmdWithLocalReturn () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwm_interpreter.so
#22 0x00d943fc in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwbridge.so
#23 0x00d951d5 in mnParser () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwbridge.so
#24 0x00d2215e in mcrInstance::mnParser () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwmcr.so
#25 0x080491c8 in __gxx_personality_v0 () at
../../../../../../sources/gcc/libstdc++-v3/libsupc++/eh_personality.cc:349
#26 0x00d1d78a in ?? () from
/usr/local/matlabR2008b/bin/glnx86/../../bin/glnx86/libmwmcr.so
#27 0x0067049b in start_thread () from /lib/libpthread.so.0
#28 0x005c742e in clone () from /lib/libc.so.6

Thread 13
#0 0x006738b0 in pthread_mutex_unlock () from /lib/libpthread.so.0
#1 0x0260f1f1 in boost::call_once<void (*)()> () from
/usr/lib/libboost_thread-gcc41-mt-1_38.so.1.38.0
#2 0x0260beda in boost::detail::set_current_thread_data () from
/usr/lib/libboost_thread-gcc41-mt-1_38.so.1.38.0
#3 0x0260cff3 in thread_proxy () from
/usr/lib/libboost_thread-gcc41-mt-1_38.so.1.38.0
#4 0x0067049b in start_thread () from /lib/libpthread.so.0
#5 0x005c742e in clone () from /lib/libc.so.6

Just for fun I wrote another program to see if the problem was anything to
do with boost mutexes or conditions (without using other threads), and
matlab has no issues with this.

-- 
View this message in context: http://www.nabble.com/Creating-a-boost-thread-in-a-matlab-mex-file-causes-matlab-to-crash-tp24163842p24199392.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net