Thread: pthread with c

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    13

    pthread with c

    i tried to make a pthread but it does not work properly .
    Code:
    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    
    void * thread1(int N,long long int array[])
    {
            while(1){
                   int i;
    			   long long int T1=0;
    
    
    	for(i=0; i<N; i++)
    	{
    		printf("array[%d]=%lld\n",i,array[i]);
    		T1=T1+array[i];
    	}
    	printf("T1=%lld\n",T1);
            }
    }
    
    
    void * thread2(int N, long long int array[])
    {
            while(1){
    
    
    {
        int i;
        long long int T2=1;
    
    
    	for(i=0; i<N; i++)
    	{
    		T2=T2*array[i];
    	}
    	printf("T2=%lld\n",T2);
    	return T2;
    }
            }
    }
    
    
    void * thread4(long long int P4array[])
    {
            while(1){
                    int i,j;
    	long long int temp=0;
    
    
        for(i=0; i<2; i++)
        {
            for(j=i+1; j<3; j++)
            {
               if(P4array[j] > P4array[i])
                {
                    temp=P4array[i];
                    P4array[i]=P4array[j];
                    P4array[j]=temp;
                }
            }
        }
        for(i=0; i<3; i++)
        {
            printf("\nP4array[%d]=%lld\n",i,P4array[i]);
        }
            }
    }
    
    
    
    
    void * thread3()
    {
            while(1){
                   long long  int T3;
                    T3 = rand()%2001+(-1000);
                    return T3;
            }
    }
    int main()
    {
            int status;
            int N,i;
    	    long long int T1=0,T3=0,*array,T2=1;
            pthread_t tid1,tid2,tid3,tid4;
    
    
            printf("Enter number of elements:");
            scanf("%d",&N);
    
    
    	array=(int*)malloc(N*sizeof(long long int));
    		 if(array==NULL)
        {
    
    
            exit(0);
        }
    
    
    
    
           srand(time(NULL));
    
    
            for(i=0;i<N;i++){
                    array[i]=(rand()%201+ (-100));
                    printf("%d\n", array[i]);}
    
    
            pthread_create(&tid1,NULL,thread1,NULL);
            pthread_create(&tid2,NULL,thread2,NULL);
            pthread_create(&tid3,NULL,thread3,NULL);
            pthread_create(&tid4,NULL,thread4,NULL);
            pthread_join(tid1,NULL);
            pthread_join(tid2,NULL);
            pthread_join(tid3,NULL);
            pthread_join(tid4,NULL);
            free(array);
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    You need to learn how to indent code before going much further.

    Clear and consistent indentation is key to understanding.
    Code:
    #include <stdio.h>
    #include <pthread.h>
    #include <stdlib.h>
    
    
    void *thread1(int N, long long int array[])
    {
      while (1) {
        int i;
        long long int T1 = 0;
        for (i = 0; i < N; i++) {
          printf("array[%d]=%lld\n", i, array[i]);
          T1 = T1 + array[i];
        }
        printf("T1=%lld\n", T1);
      }
    }
    
    
    void *thread2(int N, long long int array[])
    {
      while (1) {
        {
          int i;
          long long int T2 = 1;
    
          for (i = 0; i < N; i++) {
            T2 = T2 * array[i];
          }
          printf("T2=%lld\n", T2);
          return T2;
        }
      }
    }
    
    
    void *thread4(long long int P4array[])
    {
      while (1) {
        int i, j;
        long long int temp = 0;
    
        for (i = 0; i < 2; i++) {
          for (j = i + 1; j < 3; j++) {
            if (P4array[j] > P4array[i]) {
              temp = P4array[i];
              P4array[i] = P4array[j];
              P4array[j] = temp;
            }
          }
        }
        for (i = 0; i < 3; i++) {
          printf("\nP4array[%d]=%lld\n", i, P4array[i]);
        }
      }
    }
    
    
    void *thread3()
    {
      while (1) {
        long long int T3;
        T3 = rand() % 2001 + (-1000);
        return T3;
      }
    }
    
    int main()
    {
      int status;
      int N, i;
      long long int T1 = 0, T3 = 0, *array, T2 = 1;
      pthread_t tid1, tid2, tid3, tid4;
    
      printf("Enter number of elements:");
      scanf("%d", &N);
    
      array = (int *) malloc(N * sizeof(long long int));
      if (array == NULL) {
        exit(0);
      }
    
      srand(time(NULL));
    
      for (i = 0; i < N; i++) {
        array[i] = (rand() % 201 + (-100));
        printf("%d\n", array[i]);
      }
    
      pthread_create(&tid1, NULL, thread1, NULL);
      pthread_create(&tid2, NULL, thread2, NULL);
      pthread_create(&tid3, NULL, thread3, NULL);
      pthread_create(&tid4, NULL, thread4, NULL);
      pthread_join(tid1, NULL);
      pthread_join(tid2, NULL);
      pthread_join(tid3, NULL);
      pthread_join(tid4, NULL);
      free(array);
      return 0;
    }
    > void *thread1(int N, long long int array[])
    You can't invent your own parameter lists.
    All thread functions are of the form
    void *threadFunc(void *arg);

    Why do all your thread functions have while(1) loops, when you have things like return in the middle of the loops?

    I suggest a few simpler exercises along the lines of passing a string to a thread, which prints it out and returns a different string for main to print as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About pthread programming
    By sfaxiano in forum C Programming
    Replies: 1
    Last Post: 01-24-2014, 06:10 AM
  2. Replies: 5
    Last Post: 11-06-2013, 04:31 PM
  3. pthread question
    By Overworked_PhD in forum Linux Programming
    Replies: 2
    Last Post: 08-10-2008, 09:52 AM
  4. pthread and multiple CPU
    By noelloen in forum C Programming
    Replies: 4
    Last Post: 03-07-2006, 12:00 PM
  5. Using pthread
    By mmondok in forum C Programming
    Replies: 3
    Last Post: 03-17-2003, 12:14 PM