multithreading - [C]Malloc problems -
i'm trying write program uses basic threading assignment. below relevant snippets think causing problem.
the program runs fine 25 or less threads results in segfault when 26 or more used. led me think malloc statement incorrect. can nudge me in right direction? if need more code posted, i'd happy provide it.
also, these problems come when run program on school's student machines. appears work fine local machine. might know why?
thanks time!
... struct thread_args { struct bitmap *bm; double xmin; double xmax; double ymin; double ymax; int max; int start; int end; }; ... int num_threads; //given user input struct bitmap *bm = bitmap_create(500, 500); //all threads share same bitmap int i; pthread_t *thread_id = malloc(num_threads * sizeof(*thread_id)); struct thread_args *args = malloc(num_threads * sizeof(*args)); (i = 0; < num_threads; i++) { args[i].bm = bm; args[i].xmin = xcenter-scale; args[i].xmax = xcenter+scale; args[i].ymin = ycenter-scale; args[i].ymax = ycenter+scale; args[i].max = max; args[i].start = bitmap_height(bm) * / num_threads; args[i].end = bitmap_height(bm) * (i + 1) / num_threads; pthread_create(&thread_id[i], null, compute_image, &args[i]); } (i = 0; < num_threads; i++) { pthread_join(thread_id[i], null); } ... void* compute_image(void *arg) { struct thread_args* args = (struct thread_args*) arg; int i,j; int width = bitmap_width(args->bm); int height = bitmap_height(args->bm); // every pixel in image... for(j=args->start;j<args->end;j++) { for(i=0;i<width;i++) { // determine point in x,y space pixel. double x = args->xmin + i*(args->xmax-args->xmin)/width; double y = args->ymin + j*(args->ymax-args->ymin)/height; // compute iterations @ point. int iters = iterations_at_point(x,y,args->max); // set pixel in bitmap. bitmap_set(args->bm,i,j,iters); } } return 0; } ... valgrind log ==24919== ==24919== heap summary: ==24919== in use @ exit: 1,000,000 bytes in 1 blocks ==24919== total heap usage: 56 allocs, 55 frees, 1,018,884 bytes allocated ==24919== ==24919== searching pointers 1 not-freed blocks ==24919== checked 87,112 bytes ==24919== ==24919== 1,000,000 bytes in 1 blocks lost in loss record 1 of 1 ==24919== @ 0x4a069ee: malloc (vg_replace_malloc.c:270) ==24919== 0x401256: bitmap_create (bitmap.c:21) ==24919== 0x400cec: main (mandel.c:103) ==24919== ==24919== leak summary: ==24919== lost: 1,000,000 bytes in 1 blocks ==24919== indirectly lost: 0 bytes in 0 blocks ==24919== possibly lost: 0 bytes in 0 blocks ==24919== still reachable: 0 bytes in 0 blocks ==24919== suppressed: 0 bytes in 0 blocks ==24919== ==24919== error summary: 1 errors 1 contexts (suppressed: 6 6) --24919-- --24919-- used_suppression: 4 u1004-arm-_dl_relocate_object --24919-- used_suppression: 2 glibc-2.5.x-on-suse-10.2-(ppc)-2a ==24919== ==24919== error summary: 1 errors 1 contexts (suppressed: 6 6)
edit: added compute_image code , valgrind log although log missing error messages showing earlier today. 1,000,000 bytes lost know about.
make sure pthread_create
not returning error. machine has global limit on number of threads can spawned, , fluctuating close limit. if fail spawn thread, have garbage pthread_t
, cause pthread_join
blow up.
Comments
Post a Comment