Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using threads in C

Options
  • 12-03-2011 3:34am
    #1
    Registered Users Posts: 59 ✭✭


    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....


Comments

  • Registered Users Posts: 1,379 ✭✭✭TheCosmicFrog


    And this is why I don't do C...


  • Registered Users Posts: 441 ✭✭robfitz


    heepie wrote: »
    I'm passing iret1 from thread1 into thread2 to let thread2 know once thread one has finnished.

    The return value only tells you that the create was successful, not if it has finished.

    Try using \n instead of \r to output a new line.

    The loop only runs once because iret1 is always zero which is cast to void pointer, in update_screen the pointer is cast to an int pointer thread1 but it's value is still 0, and will never change.


  • Registered Users Posts: 59 ✭✭heepie


    Sorry for the late reply but I already finish the assignment making this only relevant for my own interest.

    I have to say that I tried teens of different combinations possible but none of them with 100% result.
    robfitz wrote: »
    The return value only tells you that the create was successful, not if it has finished.

    As advised by this and this, and countless other places I look. iret is suppouse to return 0 once the theard has finished succesfully.
    Try using \n instead of \r to output a new line.

    \r is to print on the same line making a little animation possible (-, \, |, /)
    The loop only runs once because iret1 is always zero which is cast to void pointer, in update_screen the pointer is cast to an int pointer thread1 but it's value is still 0, and will never change.

    If iret just return 0 once, when the thread is first created. How do I know thread 1 has finished?

    I also tried killing the thread or using cancel the end it but they didn't work. How do I finnish the thread2 (updating_the_screen) once thread1 finnished?


  • Registered Users Posts: 1,831 ✭✭✭dloob


    pthread_create will return 0 on success.
    What they mean by success is the thread is created, it may still be running.
    pthread_create man page

    Also notice that the iret is stored in a local variable and the value is returned as a value not a reference so the thread will not have a pointer to it and will be unable to change it.

    What you could do is have a flag variable passed to both threads and have thread one update it when it finishes.
    Just use an int and pass a pointer to it to both threads.

    A few other things
    while(thread1 != 0);
    
    is looking at a memory address not the int.
    To look at the int change to
    while(*thread1 != 0);
    

    Also this line
    iret2 = pthread_create( &thread2, NULL, *update_screen, (void *) iret1);
    
    is passing the value of iret1 cast to void * not the address of iret1 it should be
    iret2 = pthread_create( &thread2, NULL, *update_screen, (void *)&iret1);
    


Advertisement