-1

I'm very stuck with one single problem, I need prinft file's line one by one in order with one thread. This is the code:

   #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <string.h>
    /* Prototypes */

    pthread_t reader;

    void *readLine(void *line)
    {   
        printf("Thread: %s \n",(char*)line);
        pthread_exit(NULL);
    }

    int main(int argc, char *argv[])
    {
        FILE * fp;
        char * line = NULL;
        size_t len = 0;
        ssize_t read;



        fp = fopen("texto.txt", "r");
        if (fp == NULL)
            exit(EXIT_FAILURE);



        while ((read = getline(&line, &len, fp)) != -1)   {

            if(pthread_create(&reader, NULL, readLine, (void *)line)) {
                    fprintf(stderr, "Error creating thread\n");         
            }

        }


        fclose(fp);
        if  (line)
            free(line);
        exit(EXIT_SUCCESS);
    }

This code print lines in disorder and no all thread are print the line passed by readLine, how can I print line by line by the same thread reader?

StAx
  • 272
  • 1
  • 3
  • 16
  • 3
    Why would you be using `pthread` if all you have to do is print the lines one by one? I'm pretty sure that if you spawn a bunch of threads and the job of each is to print a single line, some of the threads are going to finish execution "out of order". – APerson Dec 22 '15 at 04:17
  • It would help if you didn't pass the same line buffer to all of them. Think about it - what if you start the thread, then you read the next line into the same buffer, THEN the thread calls `printf`? – user253751 Dec 22 '15 at 04:19
  • i just one part of the work, but the principal is pass line by thread to another threads. – StAx Dec 22 '15 at 04:22
  • 2
    In addition to what immibis said, if you want anything to happen in a certain order in a multi-threaded environment then you need explicit synchronisation. It's not clear exactly how you want your program to behave. But one way to serialise the output is to call `pthread_join` after the `pthread_create` to wait for the thread to complete its work. The result is a pretty useless program as the same could be done more efficiently without threading. But it will produce output in the right order. – kaylum Dec 22 '15 at 04:23
  • kaylum - :O Thanks! i already put ptrhead_join (but i think, not in the right position), with pthrad_join after create work perfectly. TY – StAx Dec 22 '15 at 04:25

1 Answers1

2

Basically, by using independent threads, you are sending them all on their way independently, and they might pass each other.

Imagine you send twenty people independently to run to Starbucks and get a coffee - depending on their driving and route choices, they will arrive in any order, not repeatable, and not in the order you sent them.

To stay in the example, just send one guy to get twenty coffees, and you will be good.

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • *Imagine you send twenty people independently to run to Starbucks and get a coffee - depending on their driving and route choices, they will arrive in any order, not repeatable, and not in the order you sent them.* And in this case, you drive off before they come back. – Andrew Henle Dec 23 '15 at 23:58