C Threads Getting Ahead of Input Parsing

Viewed 32

When removing the for loop that prints the contents of the input file I can get 0 as the count from each of the thread functions. If the for loop is there, the threads give me the correct output. It is my understanding that this is a timing related error. Is there any way to have the threads wait a second for the input file to be read in before?

int main() {

    //code that reads in file
    ...   
    ...
    //creates threads
    pthread_create(&tid0, NULL, EasyThreadFunc, (void *)&ThreadData);
    pthread_create(&tid1, NULL, PolarThreadFunc, (void *)&ThreadData);

    //prints words and # of instaces of key words
    
    for (int i=0; i < ThreadData.word_count; i++) {
        printf("%s\n",(ThreadData.str_array[i]));
    }
    printf("there was/were %d instanced of easy and %d instanced of polar",ThreadData.easy_count, ThreadData.polar_count);
    ...
    fclose(file);

    return 0;
}
0 Answers
Related