0

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!

2
  • "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? Commented Dec 7, 2014 at 16:16
  • You're right... I know I should be more specific, I was trying to confirm if I understood the flow of the threads or not, I'll edit the main post Commented Dec 7, 2014 at 16:35

1 Answer 1

2
rc = pthread_create(&threads[i], NULL, buscarExp(i,0), NULL);

Is not going to work. If you look at the documentation for pthread_create, you'll see that the third parameter (start_routine) is supposed to be a function pointer. However, your code is first calling buscarExp(i,0), then trying to pass that (incorrectly-typed) result as the thread function to pthread_create.

You need to pass a function with a compatible signature to pthread_create, using the void* to pass it any additional parameters.

Also, your main thread is exiting immediately, which is a problem, because the data I passed lives on the stack of main(). Most likely, you want to pthread_join with all of your threads after you start them.

Something like this:

struct thread_data {
    int thread_num;
    // Other things you want to pass here
}

void *thread_func(void *_data) {
    struct thread_data *data = _data;

    buscarExp(data->thread_num, 0)
}

int main (void) {

    pthread_t threads[NUM_THREADS];
    struct thread_data thread_data[NUM_THREADS];

    int i = 0, rc, a = 0;

    creatGraph();

    /* Start all threads */
    for (i=0; i<NUM_THREADS; ++i) {
        thread_data[0].thread_num = i;
        rc = pthread_create(&threads[i], NULL, thread_func, &thread_data[i]);
        if (rc) {
            printf("ERROR al crear el funcionari %d\n,",i);
            exit(-1);
        }
    }

    /* Wait for all threads to finish */
    for (i=0; i<NUM_THREADS; ++i) {
        pthread_join(&threads[i], NULL);
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Okay this is what I was looking for. Thank you! One last question though, if I understood your code right, this will create NUM_THREADS and run buscarExp on each of them, when they are done with buscarExp they will finish and everything will be over, right?
Yes. I assumed that is where your code was heading.
Okay I fully understood (I think) how pthreads work, Thanks Jonathon!
You're welcome. Welcome to the wonderful, terrible world of multithreading!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.