I am currently trying to make threads from other threads. When my threads are trying to join, I get a segfault
This is my main function:
int main( int argc, char *argv[] ) {
std::cout<< "start" << std::endl;
init();
std::cout<<"finished init" << std::endl;
t1=clock();
pthread_t threads[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
pthread_create(&threads[i], NULL, &threadMain, (void*)((long)i));
}
for (int i = 0; i < THREAD_COUNT; i++) {
printf("joining %d \n" , i);
pthread_join(threads[i], NULL);
}
timeEnd();
return(0);
}
and my thread main:
void *threadMain(void *arg) {
long thread = (long) arg;
volatile int *tix;
tix = (volatile int *)malloc(sizeof(volatile int) * INNER_THREADS);
volatile int *c;
c = (volatile int *)malloc(sizeof(volatile int) * INNER_THREADS);
volatile int r;
memset((void*)tix, 0, sizeof(tix));
memset((void*)c, 0, sizeof(c));
r = 0;
pthread_t threads[INNER_THREADS];
for (int i = 0; i < INNER_THREADS; ++i) {
vec[i+thread*2] = new desc();
vec[i+thread*2]->outterThread = thread;
vec[i+thread*2]->innerThread = i;
vec[i+thread*2]->tix = tix;
vec[i+thread*2]->c = c;
vec[i+thread*2]->r = &r;
pthread_create(&threads[i], NULL, &threadBody, (void*) vec[i+thread*2]);
}
for (int i = 0; i < INNER_THREADS; ++i) {
pthread_join(threads[i], NULL);
}
return 0;
}
Running valgrind gives me the error:
==820== Thread 288:
==820== Invalid read of size 4
==820== at 0x387AA08213: pthread_join (in /lib64/libpthread-2.12.so)
==820== by 0x4019F1: threadMain(void*) (t.c:146)
==820== by 0x387AA07AA0: start_thread (in /lib64/libpthread-2.12.so)
==820== by 0x387A2E8AAC: clone (in /lib64/libc-2.12.so)
==820== Address 0xc29c79d0 is not stack'd, malloc'd or (recently) free'd
==820==
==820==
==820== Process terminating with default action of signal 11 (SIGSEGV)
==820== Access not within mapped region at address 0xC29C79D0
==820== at 0x387AA08213: pthread_join (in /lib64/libpthread-2.12.so)
==820== by 0x4019F1: threadMain(void*) (t.c:146)
==820== by 0x387AA07AA0: start_thread (in /lib64/libpthread-2.12.so)
==820== by 0x387A2E8AAC: clone (in /lib64/libc-2.12.so)
==820== If you believe this happened as a result of a stack
==820== overflow in your program's main thread (unlikely but
==820== possible), you can try to increase the size of the
==820== main thread stack using the --main-stacksize= flag.
==820== The main thread stack size used in this run was 10485760.
Whenever I run it on an ubuntu machine I get a segmentation fault. Using gdb, putting a breakpoint in the pthread_join and stepping through eventually gives me a segfault.
Running it on a mac, I get the following output:
./a.out
start
finished init
joining 0
a.out(3473,0x10c61e000) malloc: * error for object 0x7f97fa5016f0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug
Abort trap: 6
EDIT
Some definitions:
#define INNER_THREADS 2
#define THREAD_COUNT 10
struct desc{
long outterThread;
long innerThread;
volatile int* tix;
volatile int* c;
volatile int* r;
};
struct desc* vec[THREAD_COUNT*2];