Search code examples
cmultithreadingpthreadsmutexfgets

reading file using multi threads


I wanna check a list of ips if they are blacklisted (using multi-threads).

So, I have the following code:

pthread_mutex_t input_queue;

void * process(void * data)
{
    unsigned long ip = 0xffffffff;
    char line[20];
    while (!feof(INFILE))
    {
        pthread_mutex_lock(&input_queue);//?required
        if (fgets(line,sizeof(line),INFILE) != NULL)
        {
            if (strlen(line) < 8)
                break;
            if (line[strlen (line) - 1] == '\n')
                line[strlen (line) - 1] = '\0';
            ip = ntohl((unsigned long)inet_addr(line));
        }
        pthread_mutex_unlock(&input_queue);
        blacklist(ip);
    }
    return NULL;
}

//in main()
    pthread_mutex_init(&input_queue,NULL);
    for(i = 0 ; i < number_thread; i++)
    {
        if(pthread_create(&thread_id[i],NULL,&process,NULL) != 0)
        {
            i--;
            fprintf(stderr,RED "\nError in creating thread\n" NONE);
        }
    }
    for(i = 0 ; i < number_thread; i++)
        if(pthread_join(thread_id[i],NULL) != 0)
        {
            fprintf(stderr,RED "\nError in joining thread\n" NONE);
        }

Is pthread_mutex_lock necessary or fgets is thread safe? I have the feeling that my code has some issues.


Solution

  • You don't need those. POSIX guarantees that each FILE object is thread-safe. See http://pubs.opengroup.org/onlinepubs/009695399/functions/flockfile.html:

    All functions that reference (FILE *) objects shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these (FILE *) objects.

    Unless blacklist(ip) is computation intensive, locking every 10 bytes will actually make your application much slower than avoiding multi threading altogether.