0

I could use some help passing this file pointer to a thread to have the thread write to an output file. Currently the file is not being written to at all and I am not getting any errors and working.

void *ThreadTwo(void *param){
    FILE *outputFile = param;
        // Condition for even
        if (r == 0) {
            fprintf(outputFile, "%d\n", globalValue);
            fprintf(outputFile, "%d\n", globalValue);
            printf("twice becase it is even\n");
        }
        else {
            fprintf(outputFile, "%d\n", globalValue);
            printf("Once because it is odd\n");
        }
    }
    pthread_exit(NULL);
}

int main() {
    FILE *outputFile;
    outputFile = fopen("hw3.out", "w");

    pthread_create(&th[1], NULL, ThreadTwo, &outputFile);

    fclose(outputFile);
    return 0;
}
  • 1
    Typically you use condition variables to wake up the other process. See the documentation for `pthread_cond_signal` and `pthread_cond_wait`. Warning: pthread condition variables are a real pain. Getting them right is hard to do. You might want to search for `[c] pthread condition variable` and see if you can find a credible answer that shows how to use them correctly. – user3386109 Sep 23 '22 at 22:49
  • 1
    For example, [the accepted answer to this question](https://stackoverflow.com/questions/2763714/) gives a good overview of what needs to be done, but it only has pseudocode, not real code. – user3386109 Sep 23 '22 at 22:58

0 Answers0