Hi everyone i post only the core of the code which create probleam and which work with threads.
#define HR_OFF h_r-1 pthread_t *threads = NULL; int h_r = 1; int foo(int handler) { // if everything is empty alloc resources if (threads == NULL) { threads = (pthread_t*)malloc(sizeof(pthread_t)); // stuff with other variables h_r++; } else { // stuff with other variables threads = (pthread_t*)realloc(threads, sizeof(pthread_t) * h_r); h_r++; } // stuff with other variables register unsigned int counter = 0; while (pthread_create(&threads[HR_OFF], NULL, (void*)&foo2, NULL) != 0) { if (counter == MAX_TRYING) { fprintf(stderr, "THREAD_ERROR_C occurs n"); return THREAD_ERROR_C; } } return 0; } int foo2(void *data) { // stuff with other variables }
As we can see foo function create new thread and reallocate memory to store the pthread_t. Then it try to create a new thread with pthread_create as NULL as attr and arg and as function pointer a pointer to foo2;
Now the problem is that when i execute the code i have error with memory allocation when pthread_create is called that create this error message:
Program received signal SIGABRT, Aborted. __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 ../sysdeps/unix/sysv/linux/raise.c: File o directory non esistente.
and if a print backtrace using gdb
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 #1 0x00007ffff78513fa in __GI_abort () at abort.c:89 #2 0x00007ffff78939c8 in __malloc_assert ( assertion=assertion@entry=0x7ffff7983088 "(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)", file=file@entry=0x7ffff797f867 "malloc.c", line=line@entry=2406, function=function@entry=0x7ffff79838d0 <__func__.11275> "sysmalloc") at malloc.c:301 #3 0x00007ffff7895546 in sysmalloc (nb=nb@entry=288, av=0x7ffff7bb6b00 <main_arena>) at malloc.c:2403 #4 0x00007ffff789642d in _int_malloc (av=av@entry=0x7ffff7bb6b00 <main_arena>, bytes=bytes@entry=272) at malloc.c:3865 #5 0x00007ffff7898b4b in __libc_calloc (n=<optimized out>, elem_size=<optimized out>) at malloc.c:3274 #6 0x00007ffff7deaf42 in allocate_dtv (result=result@entry=0x7ffff781c700) at dl-tls.c:322 #7 0x00007ffff7deb8ce in __GI__dl_allocate_tls (mem=mem@entry=0x7ffff781c700) at dl-tls.c:539 #8 0x00007ffff7bc400c in allocate_stack (stack=<synthetic pointer>, pdp=<synthetic pointer>, attr=0x7fffffffe4b0) at allocatestack.c:580 ---Type <return> to continue, or q <return> to quit--- #9 __pthread_create_2_1 (newthread=0x55555575b4c8, attr=<optimized out>, start_routine=0x5555555564fd <pthread_fetcher_function>, arg=0x0) at pthread_create.c:539
How can i solve this problem, where is the problem.
Thanks to all for the patience and sorry for my english
Advertisement
Answer
Your thread allocation code is pointless because you only have 1 thread handle and accessing it threads[HR_OFF]
is probably out of bounds. Casting (void) * &foo2
is wrong too, because foo2
has a proper signature like it should. Also retrying failed thread creation inside of loop is not a good idea either. Note that this loop is actually endless because you never increment counter
. You should at least check failure reason before deciding to retry.