I'm really new to threads, and I have to do an assignment. I have a graph of 6 nodes and I should create threads that move from the first node to the last one. I have everything set up except the part that needs to be done in threads. I have read some tutorials on the internet but they aren't enough, I'm having some problems and I don't understand why. Anyway, here's the code:
int main (void) {
pthread_t threads[NUM_THREADS];
int i = 0, rc, a = 0;
creatGraph();
rc = pthread_create(&threads[i], NULL, buscarExp(i,0), NULL);
if (rc) {
printf("ERROR al crear el funcionari %d\n,",i);
exit(-1);
}
pthread_exit(NULL);
return 0;
}
And here are the two main rutines I need:
void buscarDespatx(int i, int actual) {
if(llista[actual].id == 0) { // ja hem arribat al despatx
pthread_exit(NULL);
//buscarExp(i,actual); // el funcionari ha deixat l'expedient i va a buscar-ne un altre
} else { // no ha arribat al despatx
int seguent = rand() % llista[actual].Npares; // trio el node pare de tots els nodes pares q tindra
llista[actual].Proces[llista[actual].membres] = 0; // trec el proces del node actual
llista[actual].membres--; // decremento el nombre de processos al noded actual
llista[llista[actual].pares[seguent]].membres++; // incremento el nombre de processos del node pare al que anira el proces actual
llista[llista[actual].pares[seguent]].Proces[llista[llista[actual].pares[seguent]].membres] = i; // afegeixo el proces actual a la llista de processos del node pare que anira el proces
buscarDespatx(i,llista[llista[actual].pares[seguent]].id);
printf("BUSCAR EXP: El node %d ha estat modificat i ha marxat el proces %d\n",actual,i);
}
}
void buscarExp(int i, int actual) {
if(llista[actual].id == -1) { // ja hem arribat al expedient
buscarDespatx(i,actual); // el funcionari te l'expedient i el va a deixar al despatx
} else { // no ha arribat a l'expedient
int node = 0;
if(llista[actual].dret != NULL) { // aquest node te dos fils
int seg = rand();
if(seg%2 == 0) { // avança pel fill esq
llista[actual].esq->membres++; // sumo un membre al seguent node
llista[actual].esq->Proces[llista[actual].esq->membres] = i; // poso el proces al seguent node
} else { // avanço pel fill dret
node = 1;
llista[actual].dret->membres++; // sumo un membre al seguent node
llista[actual].dret->Proces[llista[actual].dret->membres] = i; // poso el proces al seguent node
}
} else { // nomes te fill esquerra
llista[actual].esq->membres++; // sumo un membre al seguent node
llista[actual].esq->Proces[llista[actual].esq->membres] = i; // poso el proces al seguent node
}
llista[actual].Proces[llista[actual].membres] = 0; // elimino el funcionari del node actual
llista[actual].membres--;
printf("BUSCAR EXP: El node %d ha estat modificat i ha marxat el proces %d\n",actual,i);
if(node == 1) { // ha passat pel dret
buscarExp(i,llista[actual].dret->id);
} else { // passa per l'esquerra
buscarExp(i,llista[actual].esq->id);
}
}
}
So, if I understood how threads works, this should do the following:
The 'main' creates the pthread and it starts doing the routing "buscarExp(i,0)", then in "buscarExp(i,0)" it keeps doing this routine recursively untill it reaches the bottom of the graph if(llista[actual].id == -1) and then it goes back to the first node with the routine "buscarDespatx(i,actual)". When it fins the initial node, I use pthread_exit(NULL); to terminate the thread.
Is this how the thread I create in the main would behave if the code was 100% correct?
Thanks!
"it isn't working"is terribly insufficient. What isn't working? What are you seeing that you didn't expect? Have you added debug print messages? Have you stepped through your code in a debugger?