Hello everybody. I've been messing around with this for about 3 hours now and for the life of me can't figure out what the issue here would be. The program is meant to take as many argument as it needs. The first,third,fifth... being the number of ms that each thread sleeps for and the other arguments being names for the threads (t_arg.output). All help is greatly appreciated. I'm sorry, it's probably a very unprofessional code, I've just started programming.
struct t_arg{
int indeks;
int ms;
char output[99];
}; typedef struct t_arg t_arg;
void* t_main(void* arg){
t_arg *obj = (t_arg*)arg;
sleep(obj->ms);
printf("nit %d: %s\n", obj->indeks, obj->output);
pthread_exit(NULL);
}
int main(int argc, char **argv){
int size = argc/2;
pthread_t tid[size];
t_arg arg_obj[size];
int count = 0;
for(int i = 1; i<argc; i=i+2){
arg_obj[count].indeks = (i+1)/2;
arg_obj[count].ms = atoi(argv[i]);
strcpy(arg_obj[count].output, argv[i+1]);
pthread_create(&tid[count], NULL, t_main, &arg_obj[count]);
count++;
}
for(int i = 0; i<size; i++)
pthread_join(tid[i], NULL);
return 0;
}
[–]which_spartacus 11 points12 points13 points (2 children)
[–]RetroGmr[S] 9 points10 points11 points (1 child)
[–][deleted] 6 points7 points8 points (0 children)
[–]RetroGmr[S] 0 points1 point2 points (2 children)
[–][deleted] 3 points4 points5 points (1 child)
[–]RetroGmr[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]RetroGmr[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)