Hello everybody,
I'm stuck with pthreads. I want to create a program which will run a persistent number of threads, by that i mean, if the number of threads i want to create is 5, the program should create 5 and if any of them finish, program should create more, so the number of threads which are working should be 5. I hope you understand what i mean, with my poor english
So, my problem is, when i'm trying to pass a var to a thread (that goes like this, 1 line in a file goes to a 1 thread and so on), i'm getting wrong results. That's an example file which is opened and parsed line by line:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
That's program code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
/* THREADS */
void *thread_function();
int running_threads = 0;
pthread_mutex_t pthr_running = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t accounts = PTHREAD_MUTEX_INITIALIZER;
/***Global vars***/
main(int argc , char *argv[])
{
/* FILE */
FILE *file;
file = fopen("h.txt" , "r");
int NTHREADS = atoi(argv[1]);
pthread_t thread_id[NTHREADS];
int TERM = 0;
int i;
int linecounter = 0;
while (! TERM) {
/* Get number of threads to create */
int pthr_for_create = NTHREADS - running_threads;
/* check if we need any */
if (pthr_for_create != 0 && pthr_for_create <= NTHREADS) {
/* Start creating them */
for(i = 0; i < pthr_for_create; i++)
{
char line[100];
int loop_num = 0;
while(fgets(line , 100 , file) != NULL) {
if (linecounter == loop_num) {
linecounter++;
break;
}
else {
loop_num++;
}
}
linecounter++;
/* Create */
pthread_create( &thread_id[i], NULL, thread_function, (void *)line);
}
}
/* Let finish any thread */
sleep (1);
}
fclose(file);
exit(0);
}
void *thread_function(void *account)
{
pthread_mutex_lock( &pthr_running );
running_threads++;
pthread_mutex_unlock( &pthr_running );
printf("\tWorking -> %d , Account-> %s" , pthread_self , account);
sleep(1);
printf("\t\t%d <- Finished\n" , pthread_self);
pthread_mutex_lock( &pthr_running );
running_threads--;
pthread_mutex_unlock( &pthr_running );
void *to_exit;
pthread_exit(to_exit);
}
And that's what i've got:
Code:
Working -> 4199040 , Account-> 9
Working -> 4199040 , Account-> 9
Working -> 4199040 , Account-> 16
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
4199040 <- Finished
4199040 <- Finished
4199040 <- Finished
4199040 <- Finished
4199040 <- Finished
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
4199040 <- Finished
4199040 <- Finished
4199040 <- Finished
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
Working -> 4199040 , Account-> 20
4199040 <- Finished
4199040 <- Finished
I understand that i didn't make a check function, to check if it's a last line to pass to a thread, and we should stop, so that's why i have so many "Account-> 20", but i don't understand why it's going like "9 9 16 20 20" , sometimes it's "9 9 9 16 20", and i want it to be like "1 2 3 4 5"
Thanks for help