-
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
-
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
-
I think you probably want to use pthread_detach instead of _join
Code:
pthread_detach(thread[i]);