As is, on Win32 that outputs: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);
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.)In demo: creating thread 0 In demo: creating thread 1 thread 0: calling malloc thread 1: calling malloc
Does anyone have some advice as to how I might successfully allocate memory inside the 'TaskCode' function on Win32 ?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
In reply to [Win32] pthreads and memory allocation by syphilis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |