#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Unhappy Pthread_create > Question


    Hi,
    Could you please help me understand why the "print_xs" function doesn't print 10 times the "thread id" number ? :chomp:

    // Compiling Command: gcc pthread.c -lpthread -o pthread
    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    /*************  Prototypes   *****************/
    void* print_xs (void* unused);
    void* print_xs (void* unused);
    /*************  Globals   ********************/
    pthread_t thread_id;
    /*************  main() function ****************/ 
    /* The main program. */
    int main ()
    {
    
    	int i;
    	for(i=0;i<10;i++)
    		{
    		  	pthread_create (&thread_id, NULL, &print_xs, NULL);
    
    		}
    		
    	return 0;
    }
    
    void* print_xs (void* unused)
    {        
           	
    		        printf(" *****\n");
    			printf("pid[] is  %d",getpid());
    			printf (" thread id %lu\n", (unsigned long)thread_id);
    			printf(" *****\n");
    					
    	return NULL;
    }
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,673
    Rep Power
    1958
    The return value of pthread_create will tell you if something went wrong.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    You have to add a join and a mutex to your code.

    Mutexes are used for serializing shared resources such as memory. Anytime a global resource is accessed by more than one thread the resource should have a Mutex associated with it.

    A join is performed when one wants to wait for a thread to finish. A thread calling routine may launch multiple threads then wait for them to finish to get the results. One waits for the completion of the threads with a join.

    Also, you need an array of 10 threads not just one thread.

    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    /*************  Prototypes   *****************/
    void* print_xs (void* unused);
    void* print_xs (void* unused);
    /*************  Globals   ********************/
    pthread_t thread_id[10];
    int counter;
    pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
    /*************  main() function ****************/
    /* The main program. */
    int main (void)
    {
        int i, j;
    
        for(i=0; i<10; i++)
        {
            pthread_create (&thread_id[i], NULL, &print_xs, NULL);
        }
    
        for(j=0; j < 10; j++)
        {
            pthread_join( thread_id[j], NULL);
        }
    
        printf("Final counter value: %d\n", counter);
    
        return 0;
    }
    
    void* print_xs (void* unused)
    {
        pthread_mutex_lock( &mutex1 );
        printf(" *****\n");
        printf("pid[] is  %d\n",getpid());
        printf("Thread number %ld\n", pthread_self());
        counter++;
        printf(" *****\n");
        pthread_mutex_unlock( &mutex1 );
    
        return NULL;
    }

    Comments on this post

    • salem agrees : Nicely done

IMN logo majestic logo threadwatch logo seochat tools logo