unix - Return a value from a thread in C -
I have the thread in C which is using the Pthreads library I use to: after a thread Finished, the result of access to the local (auto) variable of the thread is undefined. Thus, the pthread_exit () value_ptr parameter value should not be used in the context of the local variables of the thread. You are returning the address of As a way to fix this, there are some such quantities that immediately sit in spring. Using the first global variable for return value, every thread is often used for thread ID too. Second, the thread is to use the parameter passed to . At the moment, you enter an array of integers, but you can easily pass into any arbitrary structure, in which those integers containing plus to return the return code anywhere. Then, before returning the thread, it can populate that part of the structure. Thirdly, that you are in your implementation based on "compatible" and mainline to change it back: Of these, the other is probably "clean", which includes low-minded global and potentially suspicious clues. pthread_exit (...) to return the value, and wait for it to be included in
main . The thing is, I was testing it and with two threads, the result of one of them was fine, but the other was something that was not understandable. As an example, both threads should return 32, one of them was OK, but other returns-123456789 can someone help me?
Zero * contar (zero * arg) {// funcion thread int i, j = 0; Int * aux = (int *) arg; Int id = * aux; Int Strip = N / T; Int inicio = id * strip; Int Fin = Inisio + Bar; Printf ("% d% d% d \ n", id, inicio, fin); For (i = inicio; i & lt; fin; i ++) {if ([VEC [i] == X) {j ++; }} Pthread_exit (& amp; j); } Int main (int argc, char * argv []) {int i, j, k; Zero * reserve; Int * aux; Int Akum = 0; N = (argc> 1)? Atoi (argv [1]): n; T = (argc> 2)? Atoi (argv [2]): t; X = (RGC & gt; 3)? Atoi (argv [3]): x; // Aloka Memoria for Vilar VEC = (Int *) Mallok (Shape (Int) * N); aux = (int *) malloc (sizeof (int) * t); // Aloka Memoria paragraph thread thread = (pthread_t *) molk (T * size (pthread_t)); // Inicializa el vector for VEC (i = 0; i
j to local variables in your thread function. This is a big no-number.
int and
zero * types, then you pass the value back To be able to use only the pointer can be it threading:
pthread_exit ((* zero *) j);
printf ("% d \ n", (int) res);
Comments
Post a Comment