Hi,

Here is the Inline::C demo, based on the wikipedia example program. It runs fine for me on Cygwin, but never gets beyond the malloc call on native Win32 - where I'm using perl-5.12.0 and mingw.org's port of gcc-4.5.2 (which ships with the pthread library included).

Note that I'm allocating with 'malloc'. Re-allocating with 'realloc' produces the same problem, as does using 'Newx' or 'Renew' instead of the standard C functions.
use warnings; use Inline C => Config => LIBS => '-lpthread', BUILD_NOISY => 1; use Inline C => <<'EOC'; #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <malloc.h> void *TaskCode(void *argument) { int tid; char * s; tid = *((int *) argument); printf("thread %d: calling malloc\n", tid); s = (char*) malloc(100); printf("thread %d: malloc called\n", tid); return NULL; } void demo (int NUM_THREADS) { pthread_t threads[NUM_THREADS]; int thread_args[NUM_THREADS]; int rc, i; /* create all threads */ for (i=0; i<NUM_THREADS; ++i) { thread_args[i] = i; printf("In demo: creating thread %d\n", i); rc = pthread_create(&threads[i], NULL, TaskCode, (void *) &threa +d_args[i]); assert(0 == rc); } /* wait for all threads to complete */ for (i=0; i<NUM_THREADS; ++i) { rc = pthread_join(threads[i], NULL); assert(0 == rc); } exit(EXIT_SUCCESS); } EOC demo(2);
As is, on Win32 that outputs:
In demo: creating thread 0 In demo: creating thread 1 thread 0: calling malloc thread 1: calling malloc
at which point it crashes. (Irrespective of how many threads I specify it always crashes when *all* threads have reached the stage of having to allocate the memory.)

If I comment out the s = (char*) malloc(100); it outputs the expected:
In demo: creating thread 0 In demo: creating thread 1 thread 0: calling malloc thread 0: malloc called thread 1: calling malloc thread 1: malloc called
Does anyone have some advice as to how I might successfully allocate memory inside the 'TaskCode' function on Win32 ?

(I'm also interested to hear any theories that explain the behaviour I'm seeing.)

Cheers,
Rob

In reply to [Win32] pthreads and memory allocation by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.