Unexpected results printing array of int pointers in C -



Unexpected results printing array of int pointers in C -

i have simple c programme uses varying number of pthreads find first n prime numbers, adding candidates found prime array. array passed arg each thread, each thread executing of code in critical section. have no issue prime checking, , printing screen of prime numbers , result_number variable found works. however, when n primes found, , array printed, find (roughly) every sec time programme executes, (variably 1 5) of prime number array elements (generally restricted < 17) printed out extremely big or negative numbers, bulk of primes printing fine. code of thread function below (not checkprime or main), else seems work fine.

also, if programme executed single thread (i.e. no sharing of array between multiple threads updating), peculiarity never occurs.

result_number, candidate, n global vars.

void *primenums(void *arg) { pthread_mutex_lock(&mutex); int *array = (int *) arg; int is_prime = 0; int j = 0; while (result_number <= n) { candidate++; is_prime = checkprime(candidate); if (is_prime == 1) { array[result_number] = candidate; if (result_number == n) { while (j < n) { printf("%d\n", array[j]); j++; } } /* test verification output; accurate */ printf("result number: %d = %d\n", result_number, candidate); result_number++; } } pthread_mutex_unlock(&mutex); }

i genuinely not believe other qs cover this, have looked (and have preferred finding reply writing own question). there chance not searching properly, admittedly.

edit: illustration unwanted output: -386877696 3 -395270400 32605 11 13 ...

continues on fine here.

i have hunch if, instead of passing in array , using global result_number, pass in array + result_number , loop on local variable: i = 0; while(i < n) {...}, may solve problem. (assuming array + result_number + n - 1 doesn't go out of bounds).

the problem using global variables hold range info each thread thread function primenums() modifies of them. if start first thread (thread #1) result_number set start of range want thread #1 process, thread #1 maintain changing value while reset give thread #2. thread #2 won't processing range want process.

i assume want each thread process separate range of indexes in array? passing pointer origin of array function , using global variable hold index array of chunk want have processed thread.

to avoid using global index have pass pointer offset middle of array want processing begin. rather pass in value points origin element (array) pass in value points element want start processing (array + result_number).

if that, within function primenums() acts if pointer passed in origin of array (even though somewhere in middle) , can run loop 0 n because have added result_number before called function.

having said suspect still not going able process array in parallel (if that's indeed trying do) because each thread relies on candidate beingness set largest value previous thread...

to protect 'candidate` beingness simultaneously changed code launches other threads (if that) can take re-create of variable after synchronize on mutex (lock). but, honest, not sure if algorithm going allow parallelise processing.

c arrays multithreading

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -