Threads

This is a discussion on Threads within the C Programming forums, part of the General Programming Boards category; Hi, I have this code: Code: int thread_count = 5; pthread_t *thread; thread = malloc(thread_count * sizeof(pthread_t)); for ( i=0; ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Threads

    Hi, I have this code:

    Code:
    int thread_count = 5;
    pthread_t *thread;
    thread = malloc(thread_count * sizeof(pthread_t));
    
    for ( i=0; i<thread_count; i++ ) {
    
    pthread_create(&thread[i], NULL, &count_character, (void*) f);
    pthread_join(thread[i], (void*) &result);
    
    
    }
    This program are reading data from file. It must using threads. Every thread print part of file content. But these threads are running in sequence. First thread ends and second starts, when second ends third starts and so on. Why this threads are not running together? I know that pthread_join must wait while thread return a value and end but that is the reason why I use array of threads.
    I cannot use thread like pthread_t thread01, thread02, .... , but dynamically, because number of threads is argument of my program (in my example above is used 5 threads for example).

    So what should I do to run threads together? How to create a dynamic array of threads?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    pthread_join means wait for the thread to finish. So your for-loop will not continue until the thread you just started has finished.

    If you create all the threads FIRST, then wait for them to finish after this loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you probably want to use pthread_detach instead of _join
    Code:
    pthread_detach(thread[i]);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21