I've never used threads in c. I'm a bit familiar using threads on other languages a never used them in C until now. I'm doing this assignment for college and even though I wasn't ask to I wanted to use them to update screen while processing something, just to let the user know something was happening on the background.
I'm creating this 2 threads:
iret1 = pthread_create( &thread1, NULL, *go_and_do_something, (void *) data);
iret2 = pthread_create( &thread2, NULL, *update_screen, (void *) iret1);
I'm passing iret1 from thread1 into thread2 to let thread2 know once thread one has finnished.
And then I wait for the 2 threads.
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
The program compiles and runs but I never get any updates on the screen:
Here are the 2 functions:
Thread1 (Is a password generating program):
This should be irrelevant but I posted it just in case...
void *go_and_do_something (void *ptr){
ChData *data;
data = (ChData *) ptr;
do{
if(generate_random_password(data) == FALSE){
printf("Something went wrong!!!");
}else{
//DBG(("\n%s", data->password));
}
}while(verify_password_requirements(data) == FALSE);
}
Thread2:
This will run while iret1 from thread1 is not 0 (finished)
void *update_screen (void *ptr){
int *thread1;
thread1 = (int *) ptr;
int c = 0;
char p[] = {'-','\\','|','/'};
printf("\n\n");
do{
fflush(stdout);
switch(c){
case 0: puts("\t- ...Please wait while generating password... -\r"); break;
case 1: puts("\t\\ ...Please wait while generating password...\\\r"); break;
case 2: puts("\t| ...Please wait while generating password... |\r"); break;
case 3: puts("\t/ ...Please wait while generating password... /\r"); break;}
//printf("\t%c ...Please wait while generating password... %c\r", p[c], p[c]);
c++;
if(c==4) c=0;
usleep(500000);
}while(thread1 != 0);
}
I've tried with puts and printf.... with puts I get something on the screen once while thread1 runs but with printf I get nothing on screen until thread 2 finishes.
Any suggestions....